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.HomeDirectoryFor 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 .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.
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.HomeDirectoryFor 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 .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.
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
windows - Why Active Directory Home Directory query returns different in Get-ADuser than in AD admin panel? - Stack Overflow
Using PowerShell, set the AD home directory for a user, given the display name - Stack Overflow
Can't seem to pull value for HomeDirectory propery from Get-ADuser
Powershell Get-ADuser homedirectory
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
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.
Use -Properties HomeDirectory at the tail end of your get-adusers cmdlet:
get-aduser -searchbase "%OUpath" -filter {homedirectory -like '\\venus*'} -Properties HomeDirectory
That should do it!
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 the actual path to the base OU.
I know the filter works. I lose about 70 users when I add the homedirectory filter vs “filter *”, and if I query each user ID individually, I can verify the homedirectory path.
Thing is, the Homedirectory value is not being exported with the rest of the data. What am I missing? Sample data attached.
sample.csv (124 Bytes)
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.
Microsoft has updated their Active Directory powershell module and it is included with RSAT. Should you not want to use a third-party's modules, the following lists the sAMAaccountName and homeDirectory attributes for all users in the "JustAnOrgUnit" OU -- pretty much the same as @nimizen's answer, just without the Quest requirement.
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=JustAnOrgUnit,DC=example,DC=com" -Filter * -Property * |
Select-Object -Property sAMAccountName,homeDirectory |
Export-CSV -Path C:\somefile.csv
Use Quest's AD cmdlets, they're free and really simplify this sort of thing.
You can get them from http://www.quest.com/powershell/activeroles-server.aspx
Once you have those loaded, try the following script but also have a read around the Get-QADUser cmdlet.
$csvfile = "C:\somefile.csv"
$root = "OU=Accounts,DC=myDomain,DC=local"
get-qaduser -SearchRoot $root `
-ObjectAttributes @{homeDirectory=’*'} -IncludeAllProperties | `
Select-Object LogonName,HomeDirectory | `
Export-Csv $csvfile
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?

