Get AD User Home Directory using PowerShell
Before proceed run the following command to import Active Directory module.
1
|
Import-Module ActiveDirectory |
The following command return the home directory path for the user Morgan.
1
2
|
Get-ADUser -Identity 'Morgan' -Properties sAMAccountName,HomeDirectory |` Select sAMAccountName,HomeDirectory |
The following command find and list all the available users in AD.
1
2
|
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‘.
1
2
|
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.
1
2
3
|
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.
1
2
3
4
|
Get-Content "C:\Users.txt" | ForEach-Object { Get-ADUser $_ -properties sAMAccountName,HomeDirectory | Select sAMAccountName,HomeDirectory } | Export-CSV "C:\UserHomeDirectory.csv" -NoTypeInformation -Encoding UTF8 |
|
HT: https://morgantechspace.com/2016/04/get-ad-user-home-directory-using-powershell.html