Powershell-move disabled computers to OU

active-directorypowershell

This prints disabled computer accounts to the screen. I want to run a conditional statement against the results and, if true, move them to my "disabled" OU.

get-adcomputer -ldapfilter "(&(objectCategory=computer)(objectClass=computer)(useraccountcontrol:1.2.840.113556.1.4.803:=2))"|select Name, enabled

Best Answer

Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(objectClass=computer)(useraccountcontrol:1.2.840.113556.1.4.803:=2))" | Move-ADObject -TargetPath "OU=disabled,DC=ad,DC=example,DC=net"

Note: This will catch all the computers currently in the OU. You'd need to limit the scope of the search to filter them out otherwise it will retry the move (I'm not sure if it'll fail for those computers, or just skip them)

TheCleaner also wanted me to mention that you can add a -Filter {(DistinguishedName -notlike "OU=disabled,DC=ad,DC=example,DC=net")} to that Get-ADComputer statement to filter out the already moved computer.