Sunil Poddar
Sunil Poddar

Reputation: 11

I need to run a script that will return to me any sAMAccountname in AD that is in uppercase initial letter

I found below command which return all sAMAccountname in AD that has all uppercase letter. I am looking for a command that will return to me any sAMAccountname in AD that is in uppercase initial letter.

Get-ADUser -Filter * | ? {$_.sAMAccountname -ceq $_.sAMAccountname.ToUpper()}

The intent is to identify all AD user accounts that have an uppercase initial letter in their SamAccountName property; e.g., Abcd (initial A letter is in uppercase) so that later we can convert them to all-lowercase.

Upvotes: 0

Views: 1208

Answers (1)

mklement0
mklement0

Reputation: 440037

You need to limit your test to the first character of the account name:

Get-ADUser -Filter * |
  Where-Object { $_.sAMAccountname[0] -ceq [char]::ToUpper($_.sAMAccountname[0]) }

A more readable alternative suggested by Ansgar Wiechers is to use the -clike operator with a wildcard pattern:

Get-ADUser -Filter * | Where-Object { $_.SamAccountName -clike '[A-Z]*' }

Caveat: This limits matching to ASCII-range letters A through Z and won't detect foreign uppercase characters such as Ö.

Upvotes: 1

Related Questions