Way back in 2013, I put together a script to move computers to an OU based on a name prefix. This worked really well for my former environment as we enforced a very strict naming convention. It did not work so well for other environments that did not use my naming convention. It didn’t handle multiple matching OUs very well. It also relied on the older Quest AD PowerShell cmdlets.
At the prompting of a few readers, I reworked this script to be more versatile and I removed the Quest AD cmdlets requirements. Change the OU location specified in line two to any OU that you want to target. For example, you can enter your New Computers / Unassigned Computers OU to automatically move computers to their appropriate locations. You can also use it to move computers from old sites to new sites after they have been renamed.
This script looks at each computer in the OU specified. It removes one character from the computer name and searches Active Directory for any other computer starting with that prefix. If no matches are found, it removes another character and searches again. You can see that searching in the orange text below.
If a single organizational unit match is found, it will move the computer to that OU. If multiple matching OUs are found, it will list the OUs. It will then cycle through each OU by asking if the computer should be moved there. In the picture above, you can see this process in the yellow, white, and green text.
The only two lines that can modify your environment are at line 30 and line 48. Both of these lines have the -whatif parameter added already. When you run the script with the -whatif parameters, you will be able to see where computers would end up being moved.
After you are comfortable with the end result, remove the -whatif parameters to make it so. If you want to set this script up to run automatically, remove lines 37-55. The script will then move any computer that matches a single OU but leave computers matching multiple OUs alone.
As always, please let me know if you have any suggestions or issues when running this script!
#The variable ($UnassignedComputersOU) should be in the distringuished name formart. EX: OU=Something,OU=SomethingELSE,DC=TEST,DC=local [string]$UnassignedComputersOU = "OU=New Computers,OU=Sites,DC=TEST,DC=local" Import-Module ActiveDirectory $UnassignedComputers = Get-ADComputer -Filter * -SearchBase $UnassignedComputersOU | sort name foreach ($UnassignedComputer in $UnassignedComputers){ write-host "" $ComputerName = $UnassignedComputer.NAME #Filter out specific computer names here. if ($ComputerName -notmatch "MININT"){ $ComputerNameLength = $ComputerName.length Clear-Variable OUs -ErrorAction SilentlyContinue while ($OUs -eq $Null){ $ComputerNameLength = $ComputerNameLength - 1 $Prefix = $ComputerName.substring(0,$ComputerNameLength) #Filter Canonicalname attribute by removing everything after last back slash, sorting for uniqueness, and removing the new computer/unassigned computer OU from list. $OUs = Get-ADComputer -Filter "Name -like '$Prefix*'" -Properties DistinguishedName | Select-Object -ExpandProperty DistinguishedName | sort $OUs = $OUs -replace '^[^,]*,','' $OUs = $OUs | Select-Object -Unique $Ous = $OUs | Where-Object {$_ -NE $UnassignedComputersOU} if ($OUs.Count -eq 1){ Write-host "Moving $ComputerName to $OUs" -ForegroundColor Green Move-ADObject -Identity $UnassignedComputer.ObjectGUID -TargetPath $OUs -whatif | Out-Null If ($? -eq $True){ break } } if ($Ous.Count -gt 1){ Write-Host "" Write-Host "Multiple OUs found for computername $ComputerName using computerprefix $Prefix" -ForegroundColor Yellow $OUs | Write-Host Write-Host "" foreach ($OU in $OUs){ $Move = Read-host "Move $ComputerName to $OU ? Type YES to move or hit enter for NO" if ($Move -eq "YES"){ Write-host "Moving $ComputerName to $OU" -ForegroundColor Green Move-ADObject -Identity $UnassignedComputer.ObjectGUID -TargetPath $OU -whatif | Out-Null If ($? -eq $True){ break } } } } else{ Write-Warning "No matching OU found for prefix:$Prefix" } } Clear-Variable OUs -ErrorAction SilentlyContinue } }
Hi, I’m not sure how active you are on this page anymore but I had some issues with this script.
The Script itself seemed to work perfectly. No errors and it found all the correct OUs. Was pretty stoked but when I check back in Active Directory it didn’t move any of the computers. Did i miss a step or the script out of date? Found it hard to believe that it found all the right OUs for each computer but failed to move any of the computers and no errors were outputted.
Thanks!
Please ignore my ignorance.
For everyone else, you have to take away whatif (which is clearly stated in the description)
Happy coding!
-David
🙂 Thank you for posting your solution and let me know if you run into any other issues with it.