I was wondering if it was possible to change the OU (organizational unit) of a user in ActiveDirectory using PowerShell. I have a script that should update many of the fields. I am using the Set-ADUser command to update, but I can't seem to find a flag that will allow me to update the OU. Below is the the Set-ADUser command I am using currently. The variables are set earlier in the script and shouldn't be relevant to the question.
set-ADUser -identity $samName -GivenName $firstName -Surname $lastName -Department $department -Description $description -Manager $manager -AccountExpirationDate $acctExp -Organization $org
I also have a script that creates users. That script allows me to set the OU. That leads me to believe that I could change the OU after creation. Below is the command I use to create the user. Again, the variables are set earlier in the script.
New-ADUser -Name $dName -SamAccountName $sam -GivenName $firstName -Surname $lastName -Path $OU -AccountPassword $passwd -ChangePasswordAtLogon $true -Department $department -Description $description -Manager $manager -Organization $org
If there is a flag for the Set-ADUser command that would be great, otherwise, any help is appreciated. Thanks.
You can use the move-adobject:
Move-ADObject 'CN=myuser,CN=Users,DC=mydomain,DC=com' -TargetPath 'OU=mynewou,DC=mydomain,DC=com'
or
Get-ADUser $name| Move-ADObject -TargetPath 'OU=mynewou,DC=mydomain,DC=com'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With