Home » PowerShell » Get Ad User Home Directory and Home Drive

Get Ad User Home Directory and Home Drive

Active Directory user has Home Directory, Home Drive, and Profile Path properties in their profile. Get-AdUser cmdlet in the Active Directory module is used to get ad user home directory and home drive for user and export to CSV file.

Using the Active Directory Users and Computers (ADUC) console snap-in, you can view the Active Directory user Home Directory, Home Drive, and profile information.

Ad User Home Directory, Drive and Profile Path
Ad User Home Directory, Drive, and Profile Path

In this article, we will discuss how to get ad user home directory, home drive, and profile path using the Get-AdUser cmdlet in PowerShell.

Get Ad User Home Directory Path in PowerShell

Use the Get-AdUser in PowerShell to get the active directory home drive and directory folder path.

Let’s understand with an example to get ad user home directory folder path.

 Get-Aduser -Identity GaryW -Properties * | Select SamAccountName,HomeDirectory,HomeDrive,ProfilePath

In the above PowerShell script, Get-AdUser uses the Identity parameter to get the active directory user properties like HomeDirectory, SamAccountName, HomeDrive, and ProfilePath.

Use Properties * to retrieve additional properties of the active directory users.

Ad User has Home Directory set to D:\GaryW, HomeDrive path is E drive and ad user Profile Path is C:\Users\garyw

The output of the above PowerShell script is:

Get Ad User Home Directory Folder Path
Get Ad User Home Directory Folder Path

Get Home Directory of Users from OU

To get a list of active directory users in an organizational unit, use the OU path to search for users in the scope.

Let’s take an example to get a home directory for users in OU.

 $OUPath = 'OU=HR,DC=SHELLPRO,DC=LOCAL'
 Get-AdUser -Filter * -SearchBase $OUPath -Properties * | Select SamAccountName,HomeDirectory,HomeDrive,ProfilePath

In the above PowerShell script, the $OUPath variable stores the Organizational Unit path.

Using the Get-AdUser in PowerShell uses -SearchBase parameter to search for the users in the specific OU and select aduser samaccountname, home directory, and profile path.

The output of the above PowerShell script to get the home directory for users in OU is:

Get home directory for users in OU
Get home directory for users in OU

In the above script output, a few aduser has home directory blank and other properties like home drive and profile path are blank as well. You can use Set-AdUser to set ad user home directory using PowerShell.

You can export the list of ad user home directory, profile path, and home drive to CSV file using the following command.

 Get-AdUser -Filter * -SearchBase $OUPath -Properties * | Select SamAccountName,HomeDirectory,HomeDrive,ProfilePath | Export-CSV -Path C:\PowerShell\adusers-home-directory.csv -NoTypeInformation

It uses the Export-CSV cmdlet in PowerShell to export a list of aduser home directory folder paths in the CSV file.

Get Ad User Home Directory Blank

Using the Get-AdUser Filter parameter, we can find all the ad users in the specific organizational unit having the home directory as blank.

 $OUPath = 'OU=HR,DC=SHELLPRO,DC=LOCAL'

Get-AdUser -Filter {-not(HomeDirectory -like "*") } -SearchBase $OUPath -Properties * | Select SamAccountName,HomeDirectory,HomeDrive,ProfilePath

In the above PowerShell script, using the Get-AdUser Filter parameter to apply filter condition to get aduser home directory like blank or an empty.

The output of the above script returns the users having a home directory blank.

SamAccountName HomeDirectory HomeDrive ProfilePath
-------------- ------------- --------- -----------
EshD
Tira.Elsa

Get-AdUser Home Directory Size

Using the Get-AdUser cmdlet, you can get aduser home directory. Use the Get-ChildItem cmdlet in PowerShell to get the home directory size.

 $aduseraccount = Get-Aduser -Identity Arons -Properties HomeDirectory

$directorysize = Get-ChildItem $aduserAccount.homeDirectory -recurse | Measure-Object -property length -sum
$directorysize 

In the above PowerShell script, Get-Aduser gets the active directory user home directory and stores it in the $aduseraccount variable.

The Get-ChildItem searches for the aduser home directory recursively and uses Measure-Object to get the home directory size of the aduser account.

The output of the above script to get aduser home directory size is:

Count    : 2
Average  :
Sum      : 5996304
Maximum  :
Minimum  :
Property : length

Conclusion

I hope the above article about how to get aduser home directory folder path in PowerShell is useful to you.

Use the Get-AdUser cmdlet in the PowerShell to get active directory users and Properties * to retrieve an additional set of users properties like HomeDirectory, HomeDrive, and ProfilePath.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.