23Jan/110
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 querySet-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:
August 21st, 2012 - 23:26
I add filter and then script works.
Get-ADUser -Filter “*” …….
August 22nd, 2012 - 07:57
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 🙂
March 7th, 2013 - 13:01
Thank you for this! The breakdown of the command is very helpful for a first time PowerShell user!