LDAP.Rename
-----------

Changes the distinguished name of an entry in the directory.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [LDAP](component_LDAP.md) | [6.0](newinversion60.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |

MBS( "LDAP.Rename"; LDAPRef; DN; NewRDN { ; NewParent; DeleteOldRdn } ) 

**MBS**( **"LDAP.Rename";** /\* Changes the distinguished name of an entry in the directory. \*/  
**$LDAPRef**; /\* The reference number for the LDAP connection.e.g. $ldap \*/   
**$DN**; /\* The distinguished name of the entry to be renamed. \*/   
**$NewRDN**; /\* The new relative distinguished name. \*/   
**$NewParent**; /\* Optional; The distinguished name of the new parent for this entry.   
This parameter enables you to move the entry to a new parent container.  
Can be empty. \*/   
**$DeleteOldRdn**) /\* Optional; 1 if the old relative distinguished name should be deleted; 0 if the old relative distinguished name should be retained.e.g. 1 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| LDAPRef | The reference number for the LDAP connection. | $ldap |  |
| DN | The distinguished name of the entry to be renamed. |  |  |
| NewRDN | The new relative distinguished name. |  |  |
| NewParent | The distinguished name of the new parent for this entry.    This parameter enables you to move the entry to a new parent container.   Can be empty. |  | Optional |
| DeleteOldRdn | 1 if the old relative distinguished name should be deleted; 0 if the old relative distinguished name should be retained. | 1 | Optional |

### Result

Returns OK or error.

### Description

Changes the distinguished name of an entry in the directory.   
This function is available effective with LDAP 3.  
### Examples

Moves an user from one group to other group:

 Set Variable \[ $userDN ; Value: "CN=testUser,OU=TestOU1,OU=Groups,DC=example,DC=com" \]  
Set Variable \[ $rdn ; Value: "CN=testUser" \]  
Set Variable \[ $targetOU ; Value: "OU=TestOU2,OU=Groups,DC=example,DC=com" \]  
Set Variable \[ $deleteOldRdn ; Value: 1 \]  
  
Set Variable \[ $r ; Value: MBS ( "LDAP.Rename" ; $ldap ; $personDN ; $rdn ; $targetOU ; $deleteOldRdn )\]  
Example script to assign an organization unit for an user:

 # ========================================   
\# Purpose:   
\# Assigns the OU of a single AD user   
\# Returns:   
\# 0 for success   
\# Error text if unsuccessful   
\# Parameters:   
\# $serverName   
\# $serverDomain   
\# $personDN   
\# $targetOU (the full DN of the target OU)   
\# Called from:   
\# (script) "Set AD OU"   
\# Author:   
\# John Munro (HJM) from Deutsche Schule Tokyo Yokohama   
\# Notes:   
\# none   
\# History:   
\# 2020-07-10 HJM - created   
\# 2021-05-20 HJM - Replaced bind code with call to LDAPServerBind (including added parameter $serverDomain to all calls)   
\# ========================================   
\#   
Set Variable \[ $! ; Value: #Assign ( Get ( ScriptParameter ) ) \]   
\#   
// # The branch in LDAP containing all active entries  
// Set Variable \[ $searchBase ; Value: "ou=DSTY Groups,dc=dsty,dc=ac,dc=jp" \]   
\#   
Set Error Capture \[ On \]  
\#   
\# If debugging these parameters will be empty so fill with test data   
If \[ $serverName = "" \]   
 Set Variable \[ $serverName ; Value: "sys-dc1" \]   
End If  
If \[ $serverName = "" \]   
 Set Variable \[ $serverDomain ; Value: "dsty.ac.jp" \]   
End If  
If \[ $personDN = "" \]   
 Set Variable \[ $personDN ; Value: "CN=DySIS testUser,OU=VerwaltungOU,OU=DSTY Groups,DC=dsty,DC=ac,DC=jp" \]   
End If  
If \[ $targetOU = "" \]   
 Set Variable \[ $targetOU ; Value: "OU=Pre-handover,OU=DSTY Groups,DC=dsty,DC=ac,DC=jp" \]   
End If  
\#   
\#   
\# Bind to LDAP   
Perform Script \[ Specified: From list ; “LDAPServerBind” ; Parameter: # ( "serverName" ; $serverName ) &amp; # ( "serverDomain" ; $serverDomain ) \]  
\# Returns $error ,$resultText , $ldap   
\#   
Set Variable \[ $! ; Value: #Assign ( Get ( ScriptResult ) ) \]   
If \[ $error &lt;&gt; 0 \]   
 Go to Layout \[ original layout ; Animation: None \]  
 Show Custom Dialog \[ "LDAP error" ; $resultText \]   
 Exit Script \[ Text Result: # ( "error" ; $error ) &amp; # ( "resultText" ; "LDAP error: " &amp; $resultText ) \]   
End If  
\#   
\#   
\# Check the targetOU is valid (exit with error if not)   
Set Variable \[ $LDAPFilter ; Value: "" \]   
Set Variable \[ $result ; Value: MBS ( "[LDAP.Search](LDAPSearch.md)" ; $ldap ; $targetOU ; "base" ; $LDAPFilter ; "" ; 0 ; 4 ; 1 ) \]   
If \[ MBS ("[LDAP.SearchResult.DistinguishedName](LDAPSearchResultDistinguishedName.md)"; $ldap ; 0 ) = $targetOU \]   
 #   
 # Check the target is an actual Organisational Unit  
 Set Variable \[ $objectClasses ; Value: MBS ("[LDAP.SearchResult.AttributeValuesByName](LDAPSearchResultAttributeValuesByName.md)" ; $ldap ; 0 ; "objectClass" ) \]   
 #   
 If \[ FilterValues ( $objectClasses ; "organizationalUnit" ) = "" \]   
 # The target is not an actual Organisational Unit so exit with error  
 Set Variable \[ $errorText ; Value: "Target is not an Organizational Unit." &amp; ¶ &amp; $targetOU &amp; ¶ &amp; $result \]   
 Show Custom Dialog \[ "LDAP Error" ; $errorText \]   
 # Cleanup  
 Set Variable \[ $releaseResult ; Value: MBS ("[LDAP.Release](LDAPRelease.md)"; $ldap ) \]   
 Exit Script \[ Text Result: "LDAP error. " &amp; $errorText \]   
 End If  
 #   
End If  
\#   
\#   
\# Check the personDN is valid and update the dn to contain the targetOU   
Set Variable \[ $LDAPFilter ; Value: "" \]   
Set Variable \[ $result ; Value: MBS ( "[LDAP.Search](LDAPSearch.md)" ; $ldap ; $personDN ; "base" ; $LDAPFilter ; "" ; 0 ; 4 ; 1 ) \]   
If \[ MBS ( "IsError" ) \]   
 Set Variable \[ $errorText ; Value: "Failed to locate the personDN." &amp; ¶ &amp; $personDN &amp; ¶ &amp; $result \]   
 Show Custom Dialog \[ "LDAP Error" ; $errorText \]   
 # Cleanup  
 Set Variable \[ $releaseResult ; Value: MBS ("[LDAP.Release](LDAPRelease.md)"; $ldap ) \]   
 Exit Script \[ Text Result: "LDAP error. " &amp; $errorText \]   
End If  
\#   
Set Variable \[ $foundDN ; Value: MBS ("[LDAP.SearchResult.DistinguishedName](LDAPSearchResultDistinguishedName.md)"; $ldap ; 0 ) \]   
If \[ $foundDN &lt;&gt; $personDN \]   
 Set Variable \[ $errorText ; Value: "The found record DN did not match personDN." &amp; ¶ &amp; "Found: " &amp; $foundDN &amp; ¶ &amp; "PersonDN: " &amp; $personDN &amp; ¶ &amp; $result \]   
 Show Custom Dialog \[ "LDAP Error" ; $errorText \]   
 # Cleanup  
 Set Variable \[ $releaseResult ; Value: MBS ("[LDAP.Release](LDAPRelease.md)"; $ldap ) \]   
 Exit Script \[ Text Result: "LDAP error. " &amp; $errorText \]   
End If  
\#   
// # Build the JSON for the modify  
// Set Variable \[ $json ; Value: "\[{ \\"operation\\": \\"Replace\\", \\"type\\": \\"distinguishedName\\", \\"value\\": \\"" &amp; $newPersonDN &amp; "\\" }\]" \]   
\#   
\# Attempt to move the user record to the new DN   
Set Variable \[ $cn ; Value: MBS ("[LDAP.SearchResult.AttributeValuesByName](LDAPSearchResultAttributeValuesByName.md)" ; $ldap ; 0 ; "cn" ) \]   
Set Variable \[ $rdn ; Value: "CN=" &amp; $cn \]   
Set Variable \[ $deleteOldRdn ; Value: 1 \]   
Set Variable \[ $result ; Value: MBS ( "LDAP.Rename" ; $ldap ; $personDN ; $rdn ; $targetOU ; $deleteOldRdn ) \]   
If \[ MBS ( "IsError" ) \]   
 Set Variable \[ $errorText ; Value: "Failed to modify the personDN." &amp; ¶ &amp; "From: " &amp; $personDN &amp; ¶ &amp; "To: " &amp; $rdn &amp; ¶ &amp; "Under: " &amp; $targetOU &amp; ¶ &amp; $result \]   
 Show Custom Dialog \[ "LDAP Error" ; $errorText \]   
 # Cleanup  
 Set Variable \[ $releaseResult ; Value: MBS ("[LDAP.Release](LDAPRelease.md)"; $ldap ) \]   
 Exit Script \[ Text Result: "LDAP error. " &amp; $errorText \]   
End If  
\#   
\#   
\# Cleanup   
Set Variable \[ $releaseResult ; Value: MBS ("[LDAP.Release](LDAPRelease.md)"; $ldap ) \]   
Go to Layout \[ original layout ; Animation: None \]  
\#   
\#   
\# Return error free result   
Exit Script \[ Text Result: 0 \]  
### See also

- [IsError](IsError.md)
- [LDAP.Release](LDAPRelease.md)
- [LDAP.Search](LDAPSearch.md)
- [LDAP.SearchResult.AttributeValue](LDAPSearchResultAttributeValue.md)
- [LDAP.SearchResult.AttributeValues](LDAPSearchResultAttributeValues.md)
- [LDAP.SearchResult.AttributeValuesByName](LDAPSearchResultAttributeValuesByName.md)
- [LDAP.SearchResult.DistinguishedName](LDAPSearchResultDistinguishedName.md)

This function checks for a license.

Created 15th December 2015 , last changed 25th February 2026

  
[LDAP.ReleaseAll](LDAPReleaseAll.md) - [LDAP.Search](LDAPSearch.md)

[HTML Version](LDAPRename.shtml)