PoSh Disable and Move AD Users
A quick and easy way to disable user accounts and move them into designated OU:
Import-Csv "C:\TEMP\users.csv" | ForEach-Object { ` $u=$_."sAMAccountName"; $l="Disabling and moving: " +$u; write-output $l; ` Get-ADUser -Identity $u | ` Disable-ADAccount -PassThru | ` Move-ADObject -TargetPath "OU=Disabled Users,OU=Organization,DC=domain,DC=local"
Input is provided via a CSV file:
users.csv (username) sAMAccountName jdoe1 jdoe2 jdoe3 jdoe4 jdoe5
To generate input file run something like this, review and edit as necessary:
Search-ADAccount –UsersOnly –AccountInactive –TimeSpan 180.00:00:00 | ` where {$_.enabled} | ` Get-ADUser | ` select sAMAccountName | ` Export-Csv -Path "C:\TEMP\users.csv"
Import-Csv "C:\TEMP\users.csv" | ForEach-Object { ` $u=$_."sAMAccountName"; $l="Disabling and moving: " +$u; write-output $l; ` Get-ADUser -Identity $u | ` Disable-ADAccount -PassThru | ` Move-ADObject -TargetPath "OU=Disabled Users,OU=Organization,DC=domain,DC=local"
Input is provided via a CSV file:
users.csv (username) sAMAccountName jdoe1 jdoe2 jdoe3 jdoe4 jdoe5
To generate input file run something like this, review and edit as necessary:
Search-ADAccount –UsersOnly –AccountInactive –TimeSpan 180.00:00:00 | ` where {$_.enabled} | ` Get-ADUser | ` select sAMAccountName | ` Export-Csv -Path "C:\TEMP\users.csv"