2

I need to list of all users in AD that do not have any entry in the NAME field (example: smith,jon)

So far I have something like below

Get-ADUser -filter {Name -eq " "} | FT SamAccountName

I know this fails because of the empty quotations.

Essentially I need a piece of code that "lists all users who have a blank name field"

If there is another way to do this via PowerShell, or if I am just slightly off track with trying the blank quotes, let me know.

Any help is appreciated.

Rex
  • 7,895
  • 3
  • 29
  • 45
Perry
  • 21
  • 1
  • 2

2 Answers2

7

There are a few ways to go about this:

Get-ADUser -LDAPFilter "(!GivenName=*)" | ft samAccountName

Get-ADUser -Filter * | Where {$_.GivenName -eq $Null -OR $_.Surname -eq $Null -OR $_.Name -eq $Null} | select samAccountName

That should work for you.

Added a second command that will check if any of the "name" fields are blank.

HostBits
  • 11,796
  • 1
  • 25
  • 39
2

If you want all users where the Name attribute is either NULL, empty or consisting only of whitespace you should be able to do so like this:

Get-ADUser -Filter * | where {[System.string]::IsNullOrEmpty(($_.Name).Trim())} | select sAMAccountName
Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95
  • I can't seem to get this command to work. Once I execute it, it prompts me to "Supply values for the following parameters : Filter". I try and supply a parameter and it fails – Perry Mar 18 '13 at 13:53
  • Just updated it, try above – Mathias R. Jessen Mar 18 '13 at 13:54
  • So I ran what you gave me, and it returned no results (which may mean I have no users with blank NAME fields, good so far). I edited your code to change $_.Name to $_.GivenName and the code blew up. Can I not just do that? – Perry Mar 18 '13 at 14:02