# Need Help Moving AD-Object to OU that has same Display Names

**URL:** <https://forums.powershell.org/t/need-help-moving-ad-object-to-ou-that-has-same-display-names/11627>\
**Category:** PowerShell Help\
**Created:** [November 6, 2018, 12:07pm UTC](https://forums.powershell.org/t/need-help-moving-ad-object-to-ou-that-has-same-display-names/11627 "2018-11-06T12:07:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![starlord](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@starlord](https://forums.powershell.org/u/starlord)\
**Post date:** [November 6, 2018, 12:07pm UTC](https://forums.powershell.org/t/need-help-moving-ad-object-to-ou-that-has-same-display-names/11627/1 "2018-11-06T12:07:15Z")

</div>

Hello,

I have a script that works perfectly if I’m moving user’s from one ou to the Disabled Users ou that have different display names, but if the Disabled Users OU has the same display name as the user being moved, it gives a can’t move ad object error the object is in use. Please help me to modify my script so that it will move the AD-Object to the Disabled Users OU even if there’s another user in Disabled Users OU with the same name. Thank you for your help.

Import-Module ActiveDirectory  
$users= Import-Csv -Path “C:\O365AccountsTermed\Test11518.csv”

$DisabledDate = Get-Date  
$LeaveDate = Get-Date -Format “dddd dd MMMM yyyy”  
$DisabledBy = Get-ADUser “$env:username” -properties Mail  
$DisabledByEmail = $DisabledBy.Mail  
$LegalHoldUser = Get-ADuser -Filter \* -SearchBase ‘ou=LegalHold,dc=xxx,dc=com’ -Properties \* | Select-object -Expand SamAccountName

$TargetOU = “ou=Disabled Users,dc=xxx,dc=com”

foreach ($user in $users)  
{  
$SamAccountName = $User.SamAccountName  
$UserDN = (Get-ADUser -Identity $User.SamAccountName).distinguishedName

Set-ADUser $User.SamAccountName -Description “Disabled by $($DisabledBy.name) on $DisabledDate per Ticket INC0065513”

$ADgroups = Get-ADPrincipalGroupMembership -Identity $User.SamAccountName | where { ($\_.Name -ne ‘DisabledUsers’) }

Add-ADGroupMember -Identity “DisabledUsers” -Members $User.SamAccountName

$group = get-adgroup “DisabledUsers” -properties @(“primaryGroupToken”)  
Get-ADuser $User.SamAccountName | Set-ADuser -replace @{primaryGroupID=$group.primaryGroupToken}

Remove-ADPrincipalGroupMembership -Identity $($User.SamAccountname) -MemberOf $ADgroups.SamAccountName -Confirm:$false

Disable-ADAccount -Identity $($User.SamAccountname)

If ($LegalHoldUser -notcontains $User.SamAccountname)  
{  
Move-ADObject -Identity $UserDN -targetpath $TargetOU  
}  
}

---

<div class="post-metadata">

**Author:** ![jseerden](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jseerden](https://forums.powershell.org/u/jseerden)\
**Post date:** [November 6, 2018, 3:13pm UTC](https://forums.powershell.org/t/need-help-moving-ad-object-to-ou-that-has-same-display-names/11627/2 "2018-11-06T15:13:13Z")

</div>

Please avoid posting duplicates. Similar topic: [https://powershell.org/forums/topic/move-adobject-to-ou-that-has-same-display-name/](https://powershell.org/forums/topic/move-adobject-to-ou-that-has-same-display-name/)

---

<div class="post-metadata">

**Author:** ![starlord](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@starlord](https://forums.powershell.org/u/starlord)\
**Post date:** [November 6, 2018, 3:15pm UTC](https://forums.powershell.org/t/need-help-moving-ad-object-to-ou-that-has-same-display-names/11627/3 "2018-11-06T15:15:16Z")

</div>

Hi John,

Sorry about that. I was thinking something was wrong with my account. I do apologize.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/need-help-moving-ad-object-to-ou-that-has-same-display-names/11627/4 "2024-05-16T20:34:48Z")

</div>


