PowerShell - Grabbing Users /w Home Directories
-
I'm working on a simple PowerShell script that'll spit out all the users that have a Home Directory currently configured. I'm playing with Get-ADUser and it's not behaving the way I'd expect. I started with the following command:
Get-ADUser -Filter 'HomeDirectory -like "*"' -Property SamAccountName,HomeDirectory,HomeDrive | export-csv -path (Join-Path $pwd HomeDirs.csv) -encoding ascii -NoTypeInformation
However, this seems to only return
2625 accounts. I know for a fact I have WAY more than2625 accounts with Home Directories. So, I took a step back by trying the following:Get-ADUser -Filter * -Property SamAccountName,HomeDirectory,HomeDrive | export-csv -path (Join-Path $pwd HomeDirs.csv) -encoding ascii -NoTypeInformation
This returns what looks like all users in AD. However, many users that have a Home Directory configured have an empty/null HomeDirectory property.
I am baffled as to why this is happening. Does anyone have any suggestions?
EDIT: Changed record count from 26 to 25...forgot to account for the CSV header.
-
The filter statement in the first command is less than correct. Use curly braces instead of single quotes. Also, consider a where statement:
get-aduser -filter * -property SamAccountName,Homedirectory | where {$_.Homedirectory -eq ''}
Change eq to neq to find the opposite (non-empty).
-
@Grey said in PowerShell - Grabbing Users /w Home Directories:
The filter statement in the first command is less than correct. Use curly braces instead of single quotes. Also, consider a where statement:
get-aduser -filter * -property SamAccountName,Homedirectory | where {$_.Homedirectory -eq ''}
Change eq to neq to find the opposite (non-empty).
Sadly, no change. Still only get 25 results when I should be getting 100+.
Get-ADUser -Filter * -Property SamAccountName,HomeDirectory,HomeDrive | where {$_.HomeDirectory -like '*file*'} | export-csv -path (Join-Path $pwd HomeDirs.csv) -encoding ascii -NoTypeInformation
I haven't a clue what I'm missing here...
-
I just had a friend of mine ( @jrc ) try this in his environment and he gets an expected number of accounts returned respective to his environment.
So it's definitely an issue with my environment. Now to figure that out.
-
@anthonyh said in PowerShell - Grabbing Users /w Home Directories:
I just had a friend of mine ( @jrc ) try this in his environment and he gets an expected number of accounts returned respective to his environment.
So it's definitely an issue with my environment. Now to figure that out.
What is your PowerShell version?
-
@dbeato said in PowerShell - Grabbing Users /w Home Directories:
@anthonyh said in PowerShell - Grabbing Users /w Home Directories:
I just had a friend of mine ( @jrc ) try this in his environment and he gets an expected number of accounts returned respective to his environment.
So it's definitely an issue with my environment. Now to figure that out.
What is your PowerShell version?
Oo, good question.
PS Z:\> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 1 14393 2828
I am running this from my 2016 DC. I'm going to try it on my 2008 R2 DC (waiting on some hardware before replacing this one /w 2016).
-
I get the right Data on PowerShell 4.1.1 on Server 2012 R2
356 rows
-
@anthonyh said in PowerShell - Grabbing Users /w Home Directories:
Get-ADUser -Filter 'HomeDirectory -like "*"' -Property SamAccountName,HomeDirectory,HomeDrive | export-csv -path (Join-Path $pwd HomeDirs.csv) -encoding ascii -NoTypeInformation
However on my Windows 10 computer it does not return anything...
-
Same result on my 2008 R2 DC. 25 accounts returned.
PS Z:\> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 2 0 -1 -1
I feel like I'm missing something obvious here...
-
@anthonyh said in PowerShell - Grabbing Users /w Home Directories:
I feel like I'm missing something obvious here...
That would be "Windows"
-
So, I created a new user and assigned it a home directory on the server I'm trying to filter for. I ran the following in PowerShell to pull all AD users:
Get-ADUser -Filter * -Property SamAccountName,HomeDirectory,HomeDrive | export-csv -path (Join-Path $pwd AllUsers.csv) -encoding ascii -NoTypeInformation
The user I created is there, but with the HomeDirectory property blank.
However, you can see that it's clearly set in AD:
I is the confused.
-
Not sure if this is related, but I also noticed that the Enabled true/false property that comes with the dump by default only has values for 39 accounts. All the rest (2,717 accounts total) have no value for the Enabled property. Hmm...
-
@anthonyh said in PowerShell - Grabbing Users /w Home Directories:
Not sure if this is related, but I also noticed that the Enabled true/false property that comes with the dump by default only has values for 39 accounts. All the rest (2,717 accounts total) have no value for the Enabled property. Hmm...
Are you running this directly from a domain controller? It shouldn't make any difference, but just in case...
-
@anthonyh Stop trying to do the same thing over and over. Definition of insanity and all that.
Take your test user, and dump all the info about it with Powershell.
Then take another user that is showing differently.
Likely you are simply looking at the wrong field, no matter what you think you are wanting.
-
@dafyre Yes, I'm running this on a DC directly.
-
@JaredBusch said in PowerShell - Grabbing Users /w Home Directories:
@anthonyh Stop trying to do the same thing over and over. Definition of insanity and all that.
Take your test user, and dump all the info about it with Powershell.
Then take another user that is showing differently.
Likely you are simply looking at the wrong field, no matter what you think you are wanting.
I disagree with your statement that I was doing "the same thing over and over" (though I don't disagree that I'm totally insane/nuts/etc.), but I digress...
Comparing two accounts, there are obviously differences since the test account I created is a bare bones account.
Test account: https://pastebin.com/eTTx74Va
"Good" account (with redacted info, of course): https://pastebin.com/PDvDUGpB
I don't see any property referencing the home directory UNC path with my test account.
-
Oh my lawd. You won't believe the fix.
"Run as administrator"
Why didn't I think of this sooner.
I knew I was missing something stupidly simple.
Even though the account in question is a Domain Admin...
-
@anthonyh said in PowerShell - Grabbing Users /w Home Directories:
Oh my lawd. You won't believe the fix.
"Run as administrator"
Why didn't I think of this sooner.
I knew I was missing something stupidly simple.
Even though the account in question is a Domain Admin...
Even has a Domain Admin, you still will have to run PowerShell as administrator.
-
@black3dynamite But why would I see some not elevated?
I checked with @jrc and he ran it as a Domain Admin but not elevated.
I am confused, but since this works it's time to move on...haha.
-
Something you can do is set your powershell shortcut to always run admin through the shortcut properties if you find yourself forgetting about it