JEAZ The bump and grind of daily SysAdmin life

23Jan/113

Set all user’s home directories with PowerShell

Get-ADUser -SearchBase "OU=Users,DC=test,DC=ad" | % { Set-ADUser $_ -HomeDrive "H:" -HomeDirectory ('\\fileserver\home\' + $_.SamAccountName) }

Let me explain how this works briefly:

Get-ADUser = Get users from AD
-SearchBase "ldap" = Root of the tree in AD to get users from
| = Pipe, provide input of query to...
% = ForEach item piped from query
Set-ADUser = Write changes to AD
$_ = item piped from query
-HomeDrive "X:" = Sets home drive letter, requires colon and quotes!!
-HomeDirectory = Home drive path
$_.SamAccountName = item piped from query's property "SamAccountName" aka username.

We chose to use the username for the folders in the home directory storage, you don't necessarily have to.

Notice the (paranthesis) which tell Powershell to take my string \\fileserver\home\ and variable $_.SamAccountName and mush them together, passing both as the final string to -HomeDirectory

Thanks #powershell on Freenode and MS documentation examples

Technet on Set-ADUser:

http://technet.microsoft.com/en-us/library/ee617215.aspx

Comments (3) Trackbacks (0)
  1. I add filter and then script works.

    Get-ADUser -Filter “*” …….

    • Without this addition the script prompts for an LDAP filter which can help make sure you don’t affect computer accounts, etc.

      You can add the -Filter “*” to indicate you don’t want to use a filter in the command, or enter * when PowerShell prompts you for the information.

      Thanks for sharing the tip 🙂

  2. Thank you for this! The breakdown of the command is very helpful for a first time PowerShell user!


Leave a comment

No trackbacks yet.