Get-ADUser filter by property length

11,430

Solution 1

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}

Solution 2

Get-ADuser | where { $_.samaccountname.length -eq 6 } | out-file $outputFile -append
Share:
11,430
Boeckm
Author by

Boeckm

Software Engineer for an energy company in IT Security. Primary experience in VB.NET and PowerShell, with a high self interest in iOS development.

Updated on July 20, 2022

Comments

  • Boeckm
    Boeckm over 1 year

    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.