Moving User Accounts from certain OU's inside of a parent OU

Hey All, for starters my PowerShell knowledge is limiited.

I have an OU that has many OU’s inside of it. The sub-OU’s have a naming convention. The ones I need to target start with “To Be” I can get the correct list of sub-OU’s by running the command

Get-ADOrganizationalUnit -SearchBase ‘OU=Users,OU=Corp,DC=Domain,DC=com’ -Filter ‘Name -like “To Be*”’ | Format-Table DistinguishedName

I can get a list of users using the ForEach-Object {Get-ADUser and supplying the common parameters.

My problem is that I cannot come up with any combination that will allow me to get all the users from all the OU’s that start with “To Be*” and move them to a single target OU.

I can either get all the OU’s or all the people from a single OU.

What would be the correct way to loop through all the OUs that start with “To Be*” and move all the users in these OU’s to a single targeted OU?

Welcome to the forum. :wave:t4:

You actually described it?! :man_shrugging:t4: Use a loop! :wink:

$SearchBase = 'OU=Users,OU=Corp,DC=Domain,DC=com'
$TargetPath = 'OU=ToBeUsers,OU=Users,OU=Corp,DC=Domain,DC=com'

Get-ADOrganizationalUnit -SearchBase $SearchBase -Filter 'Name -like "To Be*"' | 
ForEach-Object {
    Get-ADUser -Filter * -SearchBase $_.DistinguishedName |
        Move-ADObject -Identity $_.sAMAccountName -TargetPath $TargetPath 

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

Thank you for your assistance. I was totally missing it. Also thank you for the code button info. I was looking for it but was looking in the wrong place. I see it now as I type this. You have been a great help.