PowerShell Get-AdUser based on sAMAccountName

#Create array for all accounts to be stored in

accounts = @()

#Get-AdUser cmdlet to filter on sAMAccountName and only search for enabled account and then store each one in the array
Get-AdUser -filter {sAMAccountName -Like “jeff*” -and enabled -eq $true} | select-object sAMAccountName | Foreach-Object{$accounts += $_.samaccountname}

#Echo all accounts returned in the array
$accounts | Sort-Object

#Give a count of the items in the array
$accounts.Count 

 

Written By:  Jeffrey Maxan

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s