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.
Locked
User avatar
dank42
Posts: 61
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Mon Apr 25, 2016 1:08 am
Location: United Kingdom

Query empty AD Attribute

Post by dank42 »

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: 14442
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Query empty AD Attribute

Post by jvierra »

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 »

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: 14442
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Query empty AD Attribute

Post by jvierra »

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

Locked