Set-ADUser with PowerShell ActiveDirectory: Modifying a Custom OU

I was wondering if it is possible to change the user OU (organizational unit) in ActiveDirectory using PowerShell. I have a script that should update many fields. I use the Set-ADUser command to update, but I cannot find a flag that will allow me to update OU. The following is the Set-ADUser command that I am currently using. Variables are set earlier in the script and should not be relevant to the issue.

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. This script allows me to install OU. It makes me believe that I can change IT after creation. The following 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 would be appreciated. Thanks.

+6
source share
1 answer

You can use 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' 
+5
source

All Articles