PS Script Required:  move bulk users from one OU to other in same AD

Murty M
Murty M used Ask the Experts™
on
PS Script Required:  move bulk users from one OU to other in same AD.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Just all users found in the source OU?
In test mode, will only display which users it would move; remove the -WhatIf at the end to run it for real.
$sourceOU = (Get-ADOrganizationalUnit -Filter "name -eq 'Source'").DistinguishedName
$targetOU = (Get-ADOrganizationalUnit -Filter "name -eq 'Target'").DistinguishedName
Get-ADUser -Filter *  -SearchBase $sourceOU -ResultSetSize $null | Move-ADObject -TargetPath $targetOU -WhatIf

Open in new window

Author

Commented:
I have a single domain and different OUs in that domain.
There are like 2500 users in 30 OUs.
1. I need out of 2500 users, 400 users move in to Newly created OU -- First Script.
2. New users ( not part of these 2500) to add in to domain and part of new ou. -- Second Script.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
So how do you identify the users that need moving? Do you have a csv with SamAccountNames?

Author

Commented:
Yes.  I have user names.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
So assuming you have a csv with a header row and a column "SamAccountName", you can use the following.
As before in test mode, will only display which users it would move; remove the -WhatIf at the end of line 4 to run it for real.
$targetOU = (Get-ADOrganizationalUnit -Filter "name -eq 'Target'").DistinguishedName
Import-Csv -Path 'C:\Temp\MoveUsers.csv' | ForEach-Object {
	Write-Host "Processing $($_.SamAccountName) ..."
	Get-ADUser -Identity $_.SamAccountName | Move-ADObject -TargetPath $targetOU -WhatIf
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial