You can combine Get-CimInstance with Get-LocalUser:

$userName = 'other.user'
(
  Get-CimInstance Win32_UserProfile -Filter "SID = '$((Get-LocalUser $userName).Sid)'"
).LocalPath

This outputs the path of the targeted user's profile directory, such as C:\Users\other.user.

Note: The profile directory is typically, but not necessarily the same as a user's home directory - the latter can be configured to point elsewhere, such as to a network share, and is reflected in a pair of environment variables for the current user, HOMEDRIVE and HOMEPATH.


To get the true home directory:

  • If the targeted user is stored in Active Directory, the following may work (untested):

    $userName = 'other.user'
    $user = Get-ADUser $userName -Property HomeDrive, HomeDirectory
    '{0}{1}' -f $user.HomeDrive, $user.HomeDirectory
    
  • For a local-only user:

    • I'm personally not aware of a method, given that the HOMEDRIVE and HOMEPATH environment variables are dynamically added to the registry, when that user logs on to a window station (creates an OS session), under HKEY_CURRENT_USER\Volatile Environment. By contrast, if you load another user's profile file (the hidden NTUSER.DAT file located in the user's profile directory) into the registry on demand, such as via reg.exe load or via the CreateProcessWithLogon() WinAPI function (as also used by runas.exe[1]), these values are not added.

    • If someone knows if and where the home-directory information is contained in the non-volatile information of a user's profile (as accessible via the registry after loading it), do let us know.


As for what you tried:

The relevant properties of the System.DirectoryServices.DirectoryEntry (whose type accelerator is [adsi]) instance should be .Profile and .HomeDirDrive / .HomeDirectory, but at least on my Windows 10 machine they aren't populated; e.g.:

PS> ([adsi] 'WinNT://localhost/jdoe,user') | Format-List *

# ...
HomeDirectory              : {}
# ...
Profile                    : {}
HomeDirDrive               : {}
# ...

[1] Beware that something like runas.exe /profile /user:$userName cmd /c echo %HOMEDRIVE%HOMEPATH% in effect reports %HOMEDRIVE% as C: and %HOMEPATH% as \Windows\System32(!), based on the behavior of CreateProcessWithLogon(), which - strangely - sets these variables to the working directory of the launched process, which defaults to C:\Windows\System32.

Answer from mklement0 on Stack Overflow
Top answer
1 of 2
4

You can combine Get-CimInstance with Get-LocalUser:

$userName = 'other.user'
(
  Get-CimInstance Win32_UserProfile -Filter "SID = '$((Get-LocalUser $userName).Sid)'"
).LocalPath

This outputs the path of the targeted user's profile directory, such as C:\Users\other.user.

Note: The profile directory is typically, but not necessarily the same as a user's home directory - the latter can be configured to point elsewhere, such as to a network share, and is reflected in a pair of environment variables for the current user, HOMEDRIVE and HOMEPATH.


To get the true home directory:

  • If the targeted user is stored in Active Directory, the following may work (untested):

    $userName = 'other.user'
    $user = Get-ADUser $userName -Property HomeDrive, HomeDirectory
    '{0}{1}' -f $user.HomeDrive, $user.HomeDirectory
    
  • For a local-only user:

    • I'm personally not aware of a method, given that the HOMEDRIVE and HOMEPATH environment variables are dynamically added to the registry, when that user logs on to a window station (creates an OS session), under HKEY_CURRENT_USER\Volatile Environment. By contrast, if you load another user's profile file (the hidden NTUSER.DAT file located in the user's profile directory) into the registry on demand, such as via reg.exe load or via the CreateProcessWithLogon() WinAPI function (as also used by runas.exe[1]), these values are not added.

    • If someone knows if and where the home-directory information is contained in the non-volatile information of a user's profile (as accessible via the registry after loading it), do let us know.


As for what you tried:

The relevant properties of the System.DirectoryServices.DirectoryEntry (whose type accelerator is [adsi]) instance should be .Profile and .HomeDirDrive / .HomeDirectory, but at least on my Windows 10 machine they aren't populated; e.g.:

PS> ([adsi] 'WinNT://localhost/jdoe,user') | Format-List *

# ...
HomeDirectory              : {}
# ...
Profile                    : {}
HomeDirDrive               : {}
# ...

[1] Beware that something like runas.exe /profile /user:$userName cmd /c echo %HOMEDRIVE%HOMEPATH% in effect reports %HOMEDRIVE% as C: and %HOMEPATH% as \Windows\System32(!), based on the behavior of CreateProcessWithLogon(), which - strangely - sets these variables to the working directory of the launched process, which defaults to C:\Windows\System32.

2 of 2
2

The other alternative to complement mklement0's helpful answer can be to query the registry directly, this combines Get-LocalUser with Get-ItemPropertyValue:

$sid = (Get-LocalUser other.user).Sid
Get-ItemPropertyValue "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\$sid" -Name ProfileImagePath
🌐
ShellGeek
shellgeek.com › home › powershell › get ad user home directory and home drive
Get Ad User Home Directory and Home Drive - ShellGeek
April 14, 2024 - 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
Discussions

windows - Why Active Directory Home Directory query returns different in Get-ADuser than in AD admin panel? - Stack Overflow
I wanted to ask as I'm querying all users from AD whose Home Directory is in a certain directory from Powershell using Get-ADuser , and for most cases it retrieves null result. The query I run is ... More on stackoverflow.com
🌐 stackoverflow.com
Using PowerShell, set the AD home directory for a user, given the display name - Stack Overflow
I would like to set the home directory based on a csv file containing a list of usernames. I imagine there is a combination of get-user, set-user, and foreach commands that give the correct updates. Here is the code I am using but I'm unable to make the logical jump to piping this output to a Set-ADUser ... More on stackoverflow.com
🌐 stackoverflow.com
Can't seem to pull value for HomeDirectory propery from Get-ADuser
Script: #Import necessary modules Import-Module ActiveDirectory $users = get-aduser -searchbase "$OUpath" -filter {homedirectory -like "\\venus*"} | select -property name, samaccountname, Homedirectory | export-csv C:\Users\bkatz\Desktop\report.csv $OUpath in the production version contains ... More on community.spiceworks.com
🌐 community.spiceworks.com
10
4
August 21, 2015
Powershell Get-ADuser homedirectory
Try this get-content c:\users.txt | get-aduser -properties * | select-object name, sn, givenname , homedirectory Thanks Mike ... Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... The suggestion was missing closing bracket. This was an easy fix for new PS user to figure out. Thanks for the quick response! EARN REWARDS FOR ASKING, ANSWERING, AND MORE. Earn free swag for participating on the platform. ... Active Directory ... More on experts-exchange.com
🌐 experts-exchange.com
March 18, 2014
🌐
MorganTechSpace
morgantechspace.com › home › get ad user home directory using powershell
Get AD User Home Directory using PowerShell - MorganTechSpace
March 10, 2020 - The following command return the home directory path for the user Morgan. Get-ADUser -Identity 'Morgan' -Properties sAMAccountName,HomeDirectory |` Select sAMAccountName,HomeDirectory
Top answer
1 of 2
1

I think because the HomeDirectory attribute is not in the default output set from Get-ADUser, you need to add it to the required Properties aswell.
This may be part of a larger script, but from the question I fail to see why you would need this:

$DirectoryInfo = Get-Item \\Fileserver\Users
$strFilter = $DirectoryInfo.FullName + '\*'

since you already have the UNC path for the users home directories.

I cannot test this right now, but you could try like this:

$strFilter = '\\Fileserver\Users\*'
$AdUser = Get-AdUser -Filter "HomeDirectory -like $strFilter" -Properties HomeDirectory
$AdUser

or use a Where-Object to get what you want:

$strFilter = '\\Fileserver\Users\*'
$AdUser = Get-AdUser -Filter * -Properties HomeDirectory | Where-Object { $_.HomeDirectory -like $strFilter }
$AdUser


If you prefer using the -LDAPFilter rather then -Filter, then you need to escape the special characters your string may contain.

*         \2A
(         \28
)         \29
\         \5C
NUL       \00

You do this by prepending a backslash \ to each of these characters and replacing the characters themselves by their ASCII code in hex. The ( becomes \28, the backslash \ becomes \5c etc.

This uses a small function to escape these characters for a LDAP search filter:

function Escape-LdapSearchFilter([string] $Filter) {
    return $Filter -creplace '\*', '\2a' `
                   -creplace '\(', '\28' `
                   -creplace '\)', '\29' `
                   -creplace '/' , '\2f' `
                   -creplace '`0', '\00' `
                   -creplace '\\(?![0-9A-Fa-f]{2})', '\5c'
}

$strFilter = Escape-LdapSearchFilter "\\Fileserver\Users\"
# for LDAP you must use the correct attribute name, so `homeDirectory` with a lower-case `h`
$AdUser = Get-AdUser -LDAPFilter "(homeDirectory=$strFilter*)" -Properties HomeDirectory
$AdUser
2 of 2
0

I don't know what \5c is doing in that code, so please forgive my ignorance.

if \Fileserver\Users is the root directory that contains home directories, then the following code should work:

$DirectoryInfo = Get-Item \\Fileserver\Users
$strFilter = $DirectoryInfo.FullName + '\*'
$AdUser = Get-AdUser -Filter {homeDirectory -like $strFilter}
$AdUser

The -like operator needs asterisks if your string is not an exact match.

🌐
Seei
seei.biz › get-ad-user-home-directory-using-powershell
Get AD User Home Directory using PowerShell – Software Effect Enterprises, Inc
December 9, 2021 - We can easily retrieve AD user’s home directory path by using the Active Director powershell cmdlet Get-ADUser.
🌐
Blogger
directoryadmin.blogspot.com › 2018 › 04 › how-to-find-all-ad-users-with-specidfic.html
Directory Admin: How to find all AD Users with a specidfic profilepath or homeDirectory
April 18, 2018 - To find all users using this path you could expect that you can use a query like this: Get-ADUser -Filter "homedirectory -like '\\domain.com\DFSShare\User*'" -Properties homedirectory | select samaccountname, homedirectory
Find elsewhere
🌐
Experts Exchange
experts-exchange.com › questions › 28391572 › Powershell-Get-ADuser-homedirectory.html
Solved: Powershell Get-ADuser homedirectory | Experts Exchange
March 18, 2014 - Try this get-content c:\users.txt | get-aduser -properties * | select-object name, sn, givenname , homedirectory Thanks Mike ... Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... The suggestion was missing closing bracket. This was an easy fix for new PS user to figure out. Thanks for the quick response! EARN REWARDS FOR ASKING, ANSWERING, AND MORE. Earn free swag for participating on the platform. ... Active Directory (AD) is a Microsoft brand for identity-related capabilities.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 755104 › query-users-in-an-ou-grab-homedirectory-and-use-sa
Query users in an OU - grab homedirectory and use SAMAccountname to create a path (string) - Microsoft Q&A
$users = Get-ADUser -Searchbase "Accounts,DC=test,DC=LOC" -filter * Foreach ($user in $users) { $homedirectory = (Get-Aduser -Identity $user -properties homedirectory | Select homeDirectory ) $OneDrivePath = "\\test\test\" + (Get-ADUser -Identity $user -properties SAMAccountName | Select SAMAccountname) $output = New-Object -Typename psobject -Property @{ HomeDirectory = $homedirectory OneDrivePath = $OneDrivePath } } $output | Select HomeDirectory, OneDrivePath | Export-csv "C:\temp\test.csv" Windows for business | Windows Client for IT Pros | Directory services | Active Directory
🌐
Reddit
reddit.com › r/powershell › updating home folders for ad users
r/PowerShell on Reddit: Updating Home Folders For AD Users
November 16, 2022 -

Hello!

I have around 80 AD Users, that i am trying to create a homefolder for via PowerShell.

 Set-ADUser -Identity "pabr" -HomeDirectory "\\cbw-svr\home_folders$\%username%" -HomeDrive H 

I can see that the user updates in AD, but the folder is not getting created on the fileshare.

However if I do the same thing via the AD GUI, the users homefolder gets created instantly.

It seems like for some reason that the changes are not applying correctly with PowerShell.

Does anybody have an idea what I am doing wrong, or is this even possible?

Thanks in advance.

🌐
Java2Blog
java2blog.com › home › powershell › powershell ad › get ad user home directory and home drive in powershell
Get AD User Home Directory and Home Drive in PowerShell [4 Ways] - Java2Blog
October 2, 2023 - In our case, we set the HomeDirectory ... path by mapping drives; see this article. Use the Get-ADUser cmdlet to get a home directory and home drive for all users in an active directory....
🌐
Reddit
reddit.com › r/powershell › get-aduser | select-object homedirectory result is null
r/PowerShell on Reddit: get-aduser | select-object homedirectory result is null
July 10, 2015 -

i need to automate something by pulling user account/homedirectory . using the following command i'm getting everything except for the homedirectory and homefolder... i have verified in AD that they do indeed have home folders. here is a portion of the command:

Get-AdUser $un | select-object sAMAccountName, name, lastlogondate, passwordlastset, HomeDrive, HomeDirectory

why does this not return homedrive and homedirectory?

EDIT: these are disabled accounts.. that wouldn't be why would it?

Top answer
1 of 2
5
HomeDrive and HomeDirectory are extended properties. More on that below http://social.technet.microsoft.com/wiki/contents/articles/12037.active-directory-get-aduser-default-and-extended-properties.aspx You can use the properties parameter to retrieve them get-aduser Username -properties * | select-object... #to see all the properties You can then specify the properties by name -properties homedrive, homedirectory
2 of 2
1
I think that's because the default Get-AdUser doesn't return quite a few properties by default. When in doubt, try running it on one user and pipe it to Get-Member > $me = get-aduser me > $me | gm Name MemberType Definition ---- ---------- ---------- Contains Method bool Contains(string propertyName) Equals Method bool Equals(System.Object obj) GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator() GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Item ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string p... DistinguishedName Property System.String DistinguishedName {get;set;} Enabled Property System.Boolean Enabled {get;set;} GivenName Property System.String GivenName {get;set;} Name Property System.String Name {get;} ObjectClass Property System.String ObjectClass {get;set;} ObjectGUID Property System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, ... SamAccountName Property System.String SamAccountName {get;set;} SID Property System.Security.Principal.SecurityIdentifier SID {get;set;} Surname Property System.String Surname {get;set;} UserPrincipalName Property System.String UserPrincipalName {get;set;} Then I tried > $me = get-aduser me -Properties HomeDirectory > $me | gm Name MemberType Definition ---- ---------- ---------- Contains Method bool Contains(string propertyName) Equals Method bool Equals(System.Object obj) GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator() GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Item ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string ... DistinguishedName Property System.String DistinguishedName {get;set;} Enabled Property System.Boolean Enabled {get;set;} GivenName Property System.String GivenName {get;set;} HomeDirectory Property System.String HomeDirectory {get;set;} Name Property System.String Name {get;} ObjectClass Property System.String ObjectClass {get;set;} ObjectGUID Property System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral,... PSShowComputerName Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection PSShowComput... SamAccountName Property System.String SamAccountName {get;set;} SID Property System.Security.Principal.SecurityIdentifier SID {get;set;} Surname Property System.String Surname {get;set;} UserPrincipalName Property System.String UserPrincipalName {get;set;} WriteDebugStream Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection WriteDebugSt... WriteErrorStream Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection WriteErrorSt... WriteVerboseStream Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection WriteVerbose... WriteWarningStream Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection WriteWarning...
🌐
Oobabooga
archy.net › automating-home-directory-settings-for-active-directory-users-with-powershell
Automating Home Directory Settings for Active Directory Users with PowerShell
October 20, 2023 - # Define the log file path $logFilePath = "C:\PathToLogFile\AD_HomeDirectory_Setting.log" # Start transcript to log the script's output Start-Transcript -Path $logFilePath # Define the target OU and home directory path $targetOU = "OU=UsersWorldcom,DC=worldcom,DC=local" # Replace with your specific OU path $homeDirectory = "\\server\share\%username%" # Replace with the desired home directory path # Get a list of user objects in the target OU $users = Get-ADUser -Filter * -SearchBase $targetOU # Loop through each user and set the home directory value foreach ($user in $users) { # Construct the
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › activedirectory › get-aduser
Get-ADUser (ActiveDirectory) | Microsoft Learn
The Get-ADUser cmdlet gets a specified user object or performs a search to get multiple user objects. The Identity parameter specifies the Active Directory user to get. You can identify a user by its distinguished name (DN), GUID, security ...
🌐
Experts Exchange
experts-exchange.com › questions › 28231995 › Powershell-Get-ADUser-not-showing-homedrive-and-homedirectory.html
Solved: Powershell Get-ADUser not showing homedrive and homedirectory | Experts Exchange
September 5, 2013 - However the Homedrive and homedirectory properties show with no values whether I specifically look for those properties or choose to view all of the properties of the users. If I look in AD Users and Computers at the Profile tab for many of the users I do see a drive and UNC path listed for them. What is awry? Get-ADUser -SearchBase "OU=Users,OU=CityOU,DC=Com