PoSh Disable and Move AD Users

A quick and easy way to disable user accounts and move them into designated OU:

  1. Import-Csv "C:\TEMP\users.csv" | ForEach-Object { `  
  2.     $u=$_."sAMAccountName"; $l="Disabling and moving: " +$u; write-output $l; `  
  3.     Get-ADUser -Identity $u | `  
  4.     Disable-ADAccount -PassThru | `  
  5.     Move-ADObject -TargetPath "OU=Disabled Users,OU=Organization,DC=domain,DC=local"  

Input is provided via a CSV file:

users.csv (username)
  1. sAMAccountName  
  2. jdoe1  
  3. jdoe2  
  4. jdoe3  
  5. jdoe4  
  6. jdoe5  


To generate input file run something like this, review and edit as necessary:

  1. Search-ADAccount –UsersOnly –AccountInactive –TimeSpan 180.00:00:00 | `  
  2.     where {$_.enabled} | `  
  3.     Get-ADUser | `  
  4.     select sAMAccountName | `  
  5.     Export-Csv -Path "C:\TEMP\users.csv"  

Comments

Popular posts from this blog

Mail-enabled security groups in Office 365

User attributes in Office 365

Skype for Business and VTC Interoperability