Guest User

PowerShell AD

a guest
Jan 25th, 2016
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import-module activedirectory
  2.  
  3. $ou = Read-Host "What OU do you want to change?";
  4.  
  5. $usersToChange = Get-ADUser -Filter * -SearchBase "OU=$ou, OU=PBBUsers, DC=pbb, DC=corp";
  6. # Grabs all the users in the Sales OU and puts them in an array
  7.  
  8. foreach ($user in $usersToChange) {
  9.  
  10.     # We can also substitute GivenName for sAmAccountName to get the user's first Name.
  11.     $oldLogon = Get-AdUser $user -Properties * | Select-Object -Expand sAMAccountName;
  12.     # This grabs Steven.L
  13.  
  14.     $lastName = Get-AdUser $user -Properties * | Select-Object -Expand surname;
  15.     # This grabs Luengo
  16.  
  17.     $parse = $oldLogon.SubString(0,1);
  18.     # This parses out "S"
  19.  
  20.     $newLogon = $parse + $lastName;
  21.     # This concatenates "S" + "Luengo" into "SLuengo"
  22.    
  23.     $principalName = $newLogon + "@pbb.corp";
  24.    
  25.    
  26.    
  27.     Get-ADUser -Identity $user | Set-AdUser -Replace @{samaccountname = $newLogon};
  28.     Get-ADUser -Identity $user | Set-AdUser -Replace @{userPrincipalName = $principalName};
  29.    
  30.     Set-AdUser -Identity $newLogon -HomeDrive "H:" -HomeDirectory "\\sv-fs-01\users$\$oldLogon";
  31.    
  32.     Rename-Item -path "\\sv-fs-01\users$\$oldLogon" -newName "\\sv-fs-01\users$\$newLogon";
  33.     # This renames the path of the home folder for the user to match the new logon name
  34.    
  35.    
  36.     Set-AdUser -Identity $newLogon -HomeDrive "H:" -HomeDirectory "\\sv-fs-01\users$\$newLogon";
  37.    
  38.    
  39.  
  40. }
Add Comment
Please, Sign In to add comment