Rather than looking for "$env:HOMEDIR", I'm looking for a way to query the home directory of a different user account. The powershell equivalent of "~bob".
I attempted using [ADSI]:
# After creating a local account called 'other.user'
PS> $user = [ADSI]'WinNT://localhost/other.user,user'
PS> $user.name
other.user
PS> $user.HomeDirectory
PS>
even when executed from the Administrator account in an elevated powershell.
For sanity, I tried doing the same check on the existing user:
PS> $user = [ADSI]('WinNT://localhost/{0},user' -f [Environment]::UserName)
PS> $user.name
this.user # paraphrase
PS> $user.HomeDirectory
PS> $user.properties.HomeDirectory
PS>
You can combine
Get-CimInstancewithGet-LocalUser: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,
HOMEDRIVEandHOMEPATH.To get the true home directory:
If the targeted user is stored in Active Directory, the following may work (untested):
For a local-only user:
I'm personally not aware of a method, given that the
HOMEDRIVEandHOMEPATHenvironment variables are dynamically added to the registry, when that user logs on to a window station (creates an OS session), underHKEY_CURRENT_USER\Volatile Environment. By contrast, if you load another user's profile file (the hiddenNTUSER.DATfile located in the user's profile directory) into the registry on demand, such as viareg.exe loador via theCreateProcessWithLogon()WinAPI function (as also used byrunas.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.Profileand.HomeDirDrive/.HomeDirectory, but at least on my Windows 10 machine they aren't populated; e.g.:[1] Beware that something like
runas.exe /profile /user:$userName cmd /c echo %HOMEDRIVE%HOMEPATH%in effect reports%HOMEDRIVE%asC:and%HOMEPATH%as\Windows\System32(!), based on the behavior ofCreateProcessWithLogon(), which - strangely - sets these variables to the working directory of the launched process, which defaults toC:\Windows\System32.