Get AD User Home Directory using PowerShell

We can easily retrieve AD user’s home directory path by using the Active Director powershell cmdlet Get-ADUser. In this post, I am going to write powershell script get home directory path for an ad user, users from specific OU and set of users from text file.

Before proceed run the following command to import Active Directory module.

Import-Module ActiveDirectory

The following command return the home directory path for the user Morgan.

Get-ADUser -Identity 'Morgan' -Properties sAMAccountName,HomeDirectory |`
   Select sAMAccountName,HomeDirectory

The following command find and list all the available users in AD.

Get-ADUser -Filter * -Properties sAMAccountName,HomeDirectory |`
  Select sAMAccountName,HomeDirectory

Get home directory for users from specific OU

We can find and get a list of all users from a certain OU by setting target OU scope by using the parameter SearchBase. The following powershell command select home directory for all users from the Organization Unit ‘TestOU‘.

Get-ADUser -Filter * -SearchBase "OU=TestOU,DC=TestDomain,DC=com" `
 -Properties sAMAccountName,HomeDirectory | Select sAMAccountName,HomeDirectory

Export AD user details to CSV

We can export all users details to csv file by using the powershell cmdlet Export-CSV.

Get-ADUser -Filter * -SearchBase "OU=TestOU,DC=TestDomain,DC=com" `
-Properties sAMAccountName,HomeDirectory | Select sAMAccountName,HomeDirectory |
 Export-CSV "C:\UserHomeDirectory.csv" -NoTypeInformation -Encoding UTF8

Get home directory for set of users from text file

Use the below powershell script to read the home directory path for set of users from text file. First create the text file Users.txt which includes one user name (samaccountname) in each line.

Get-Content "C:\Users.txt" | 
ForEach-Object {Get-ADUser $_ -properties sAMAccountName,HomeDirectory | 
 Select sAMAccountName,HomeDirectory } |
 Export-CSV "C:\UserHomeDirectory.csv" -NoTypeInformation -Encoding UTF8

Advertisement

1 thought on “Get AD User Home Directory using PowerShell”

Leave a Comment