Update AD User Home Directory by using PowerShell

Sometimes Active Directory Administrator requires to change user’s ‘Home Folder’ profile mapping location from old file server to new file server. We can use the AD powershell cmdlet Set-ADUser to update user detail. It has a parameter -HomeDirectory , which allows you to set the user’s home directory and it also has a parameter -HomeDrive that update the drive letter for their home directory.

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:
}

Advertisement

12 thoughts on “Update AD User Home Directory by using PowerShell”

    • 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;
      }

      Reply
  1. 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”

    }

    Reply
  2. 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?

    Reply

Leave a Comment