Boeckm
Boeckm

Reputation: 3312

Get-ADUser filter by property length

I can't seem to figure this out to save my life.

I want to grab all AD users where their SAMAccountName length is equal to 6.

I'm hoping for something like this

Get-ADuser -filter "samaccountname.length -eq 6" | out-file $outputFile -append

I'm writing a massive script to first dump all AD users, then loop through each dumped user and update some attributes. This script will be run often, so I want to make it as efficient as possible. One area that I thought could be improved is the dump process.

We have about 15 thousand users in AD, but I'm only interested in 4 thousand, specifically, the ones for which their SamAccountName is 6 characters. For this reason, I don't want to fill my ID output file with about 11 thousand unnecessary IDs.

I want to try and avoid an inline for-each if possible.

Any help would be greatly appreciated.

Upvotes: 3

Views: 14230

Answers (2)

CB.
CB.

Reputation: 60918

Try this:

Get-ADuser - filter * | ? { $_.samaccountname.length -eq 6} | out-file -$outputfile -append

I usually do it with Get-QADuser (from Quest module) but I think Get-ADUser is same.

If $_.samaccountname isn't a string maybe you have to use:

$_.samaccountname.tostring().length

EDIT:

Get-ADUser -Filter * -Properties samaccountname | ? {$_.samaccountname.length -eq 6}

Upvotes: 3

oɔɯǝɹ
oɔɯǝɹ

Reputation: 7625

Get-ADuser | where { $_.samaccountname.length -eq 6 } | out-file $outputFile -append

Upvotes: 1

Related Questions