PowerShell 1 min read
How to Update the Home Directory in User Profile by PowerShell
Michael Wu ·
Some users experienced issues with unmapped drives on their computers. Investigation revealed that the Home folder path was blank in user profiles.
The Solution
# Get all users in the OU
$allfacstaff = Get-ADUser -Filter * -SearchBase "OU=FacStaff,OU=Campus,DC=mike,DC=com"
# Create a new object
$facstaff = @()
foreach ($user in $allfacstaff) {
$firstletter = $($user.surname).Substring(0, 1)
$SSOID = $user.SamAccountName
$path = "\\share\$firstletter\$SSOID"
$homedriveLetter = "K:"
$homeDirectory = Get-ADUser $SSOID -Properties * |
Select-Object -ExpandProperty HomeDirectory
# Add into object
$prop = @{
'SSOID' = $SSOID
'HomeDirectory' = $homeDirectory
}
$obj = New-Object -TypeName PSObject -Property $prop
$facstaff += $obj
if ($homeDirectory -eq $null) {
Set-ADUser -Identity $SSOID -HomeDirectory $path -HomeDrive $homedriveLetter
}
}
# Export to CSV
$facstaff | Export-Csv -Path c:\temp\mapDrive.csv -NoTypeInformation
Key Details
- The script uses an
ifcheck to see if the home directory path is blank - When blank, it assigns both a drive letter and path
- Remove the conditional to apply settings to all users
- Computers require a restart for the mapped drive to function properly