question

EavenHuang avatar image
0 Votes"
EavenHuang asked EavenHuang commented

How to move OU members into other OU based on sAMaccountname or other attribute

Dear friend,

We have an OU containing around 1000+ members and we will need to create sub-OU to separate them. We will create 10 sub-OUs and move them into different ones. I have an excel list containing the members' E-mail, sAMAccountName, can we do this in bulk based on their E-mail field or sAMAccountName attribute?

Many thanks in advance for any assistance.

windows-server
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered EavenHuang commented

Hi,

You can create a script to do this. The Move-ADObject cmdlet moves an AD object to a specific container.

 $sAMAccountName = "someuser"
 $ou = "OU=test,DC=contoso,DC=com"
 Get-ADUser -Identity $sAMAccountName | Move-ADObject -TargetPath $ou

https://docs.microsoft.com/en-us/powershell/module/activedirectory/move-adobject

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Ian,

Thanks for the information, I was looking for the same:) Is there anyway that we can read the sAMAccountName from .csv instead of putting all of them into the PS script?

0 Votes 0 ·

If the list is a CSV file you can use the Import-Csv cmdlet to read it. Assudming the header of the column is "sAMAccountName", you can get the sAMAccountName like below

 $file = "C:\temp\list.csv"
 (Import-Csv -Path $file).sAMAccountName
0 Votes 0 ·

Many thanks Ian!

I was also referring to this link https://www.alitajran.com/bulk-move-ad-users-to-another-ou-with-powershell/ and I think it's the same as your reply:)

0 Votes 0 ·