Query empty AD Attribute

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
dank42
Posts: 61
Joined: Mon Apr 25, 2016 1:08 am
Location: United Kingdom

Query empty AD Attribute

Post by dank42 » Mon Nov 18, 2019 2:54 am

Hi,

I can set the following attribute (DepartmentNumber) with the below command:
  1. Set-ADUser -Identity $user -Add @{'departmentNumber'=$Number}
But I'm trying to return all accounts where the value is empty

E.g.
  1. Get-ADUser -filter * -Properties DepartmentNumber | Where {$_.DepartmentNumber -eq ""}
The above command doesn't return any accounts, must be doing something wrong

Thanks in advance

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Query empty AD Attribute

Post by jvierra » Mon Nov 18, 2019 10:12 am

Get-ADUser -filter "DepartmentNumber -notlike '*'"

User avatar
dank42
Posts: 61
Joined: Mon Apr 25, 2016 1:08 am
Location: United Kingdom

Re: Query empty AD Attribute

Post by dank42 » Tue Nov 19, 2019 2:08 am

Many thanks :)

Maybe a stupid question but why does it look as if it doesn't recognize the comparison operator for -notlike? I get that it's wrapped in double quotes but it almost looks as if it won't work and throw a syntax error.

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Query empty AD Attribute

Post by jvierra » Tue Nov 19, 2019 2:31 am

The exact line works in all versions of PowerShell and AD.

Post Reply