Thursday, 28 April 2016

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 "\\fileServer\Users\Smith" -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 = '\\fileserver\users\' + $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 = '\\fileserver\users\' + $_.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 = '\\fileserver\users\' + $userAccount.SamAccountName;
    # Finally set their home directory and home drive letter in Active Directory
    Set-ADUser -Identity $userAccount.SamAccountName -HomeDirectory $homeDirectory -HomeDrive H


No comments:

Post a Comment