Why Active Directory Home Directory query returns different in Get-ADuser than in AD admin panel?

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 this:

$DirectoryInfo = Get-Item \\Fileserver\Users

$strFilter = $DirectoryInfo.FullName.Replace('\','\5c')

$AdUser = Get-AdUser -Filter {homeDirectory -like $strFilter}

echo $AdUser

If I look from AD admin center panel for a specific user, I can see it has set Home Directory inside path i've queried before in Powershell indeed.

Another thing that seems to me strange is that there are some users that appear in the query, so the previous case doesn't apply for all users.

Is it that AD admin center panel shows Home Directory of Group where this user belongs for example, or is just that I'm running a wrong query from Powershell?

Thanks in advance,

Juan Pablo.

1 answer

  • answered 2019-02-06 22:27 AdminOfThings

    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.