Before proceed run the following command to import Active Directory module.
Import-Module ActiveDirectory
The below powershell command set the home directory path and link home drive for the user ‘Smith‘
Set-ADUser -Identity "Smith" -HomeDirectory "fileServerUsersSmith" -HomeDrive H:
You can also find an user and set their DisplayName or samAccountName as home directory folder.
# Get the user, based on their "samAccountName" $user = Get-ADUser -LDAPFilter '(samAccountName=Smith)'; # Change the user's samAccountName as home directory $homeDirectory = 'fileserverusers' + $user.SamAccountName; Set-ADUser -Identity $user.SamAccountName -HomeDirectory $homeDirectory -HomeDrive H:
Set Home Directory for all AD users from OU
When we change user’s home folder while migrating file server, we need to update for bulk of AD users. If you placed group of users under certain OU, you can get all users from that OU by setting target OU scope in Get-ADUser cmdlet and change home directory path for every user.
$users = Get-ADUser -Filter * -SearchBase "OU=TestOU,DC=TestDomain,DC=com" $users | ForEach-Object { # Assign user's home directory path $homeDirectory = 'fileserverusers' + $_.SamAccountName; Set-ADUser -Identity $_.SamAccountName -HomeDirectory $homeDirectory -HomeDrive H: }
Update Bulk AD Users Home Directory from CSV
We can also set bulk AD users home directory path by importing user details from CSV file. First consider the csv file Users.csv which includes user’s display name or samaccountname, the following powershell script import AD user’s display name from csv file and set home directory path by using their samAccountName.
# Import user details from CSV $users = Import-Csv -Path "C:\Users.csv" # Iterate every row to set each user ... foreach ($user in $users) { # Get the user, based on their "displayName". If you have samAccountName in you csv file, # you can replace displayName by samAccountName $userAccount = Get-ADUser -LDAPFilter ('(displayname={0})' -f $user.DisplayName); # Assign user's home directory path $homeDirectory = 'fileserverusers' + $userAccount.SamAccountName; # Finally set their home directory and home drive letter in Active Directory Set-ADUser -Identity $userAccount.SamAccountName -HomeDirectory $homeDirectory -HomeDrive H: }
How is the csv to be formatted?
please see this file : http://1.bp.blogspot.com/-0x8eCuhEZjk/U1Lcz9d4oqI/AAAAAAAABBs/mAVZshMFdJk/s1600/Create-Bulk-AD-Users-From-CSV-Using-Powershell.png
Need to change from old path to new path
Hi, when I do this, the drive letter is set to Z: and not to H:. I don´t know why.
Can you ensure that the Drive "H" is not mapped with any other shared folder.
Hi, how would I Set Home Directory for all AD users from a Group?
Can you try the below commands?
$users = Get-ADGroupMember -Identity GroupName
$users | ForEach-Object {
$homeDirectory = ‘fileserverusers’ + $_.SamAccountName;
Set-ADUser -Identity $_.SamAccountName -HomeDirectory $homeDirectory -HomeDrive H;
}
Simple way using CSV file. Just create UserID and HomeDrive field in CSV, populate with your data.
UserID = AD login user ID
HomeDrive=\\servername\sharename\
——————————————————————————–
Import-Module ActiveDirectory
$users = import-csv “HomeDrive.csv”
$Results = ForEach ($user in $users)
{
$Identity = $User.UserID
$HomeDrive = $User.HomeDrive
Set-ADUser -Identity $Identity -HomeDirectory $HomeDrive$identity -HomeDrive H
Write-Host “Changing user $Identity H home drive to $HomeDrive$Identity”
}
Hi, having trouble with this. I have a list of samAccountNmkaes, but can’t get it to work.
It says:
# Get the user, based on their “displayName”. If you have samAccountName in you csv file,
# you can replace displayName by samAccountName
$userAccount = Get-ADUser -LDAPFilter (‘(displayname={0})’ -f $user.DisplayName);
What exactly do I change it to?
I tried:
$userAccount = Get-ADUser -LDAPFilter (‘(samAccountName={0})’ -f $user.samAccountName);
but it doesn’t work.
Any suggestions?
May want to edit the code and add the missing colon : to the -HomeDrive
Otherwise, thanks! Saved us a bit of time.
There should be “-HomeDrive H:” (colon after H, not semicolon and not letter alone) or it don’t work.
thanks updated