Steph_69
Steph_69

Reputation: 1

Get AD user properties from Active Directory

I am trying to filter the values of a property in Active Directory.

I tried:

 Get-ADUser -filter * -Properties physicalDeliveryOfficeName | Where-Object (($_.physicalDeliveryOfficeName -like "NICE")) | Select-Object physicalDeliveryOfficeName, name 

Get-ADUser -filter * -Properties physicalDeliveryOfficeName | Select-Object physicalDeliveryOfficeName, name | Where-Object (($_.physicalDeliveryOfficeName -like "NICE"))

I did not get any errors, but no results either.

I searched all users with physicaldeliverofficename is (myvalue). I would like to display name and office.

Upvotes: 0

Views: 1844

Answers (2)

mklement0
mklement0

Reputation: 440112

You have a syntax problem:

The Where-Object's (positionally implied) -FilterScript parameter expects a script block argument - { ... } - not a parenthesized expression ((...)).[1]

Therefore:

# Note the { ... } around the expression passed to Where-Object
Get-ADUser -Filter * -Properties physicalDeliveryOfficeName | 
  Where-Object { $_.physicalDeliveryOfficeName -eq "NICE" } # | ...

Note: Since "NICE" is a literal string rather than a wildcard pattern, I've used the -eq instead of the -like operator. If you doe need to find "NICE" as a substring, use something like -like "*NICE*" or, for case-sensitive matching, -clike "*NICE*", as Mathias R. Jessen suggests.

Note that you may alternatively use simplified syntax, which obviates the need for a script block and allows use of individual parameters (also note the absence of $_., which is implied):

Get-ADUser -Filter * -Properties physicalDeliveryOfficeName | 
  Where-Object physicalDeliveryOfficeName -eq "NICE" # | ...

Taking a step back:

Santiago Squarzon suggests performing the filtering at the source, by using Get-ADUser's -Filter or -LDAPFilter parameter, which is much more efficient; e.g.:

Get-ADUser -Filter 'physicalDeliveryOfficeName -eq "NICE"'

As an aside: There are many examples out there that use script-block syntax with -Filter (-Filter { ... }), but the -Filter parameter accepts a string and that string, even though it supports PowerShell-like syntax, is interpreted by the AD provider, so it's better to pass a string to begin with - see this answer for more information.


[1] If you use (...), the expression's value gets bound to the -Property parameter instead, and is therefore interpreted as a property name whose value - assuming such a property even exists - is interpreted as a Boolean that determines whether the input object at hand should be filtered in or not. If the expression doesn't evaluate to the name of a property that exists on an input object, $false is implied, and the input object is filtered out. In your case, this predictably resulted in no objects being filtered in and therefore no output.

Upvotes: 1

FoxDeploy
FoxDeploy

Reputation: 13567

The Select-Object cmdlet is used to select only the columns you want from a larger object or list.

For instance:

 C:\git\Core> gsv Spooler | fl

Name                : Spooler
DisplayName         : Print Spooler
Status              : Running
DependentServices   : {Fax}
ServicesDependedOn  : {RPCSS, http}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : True
ServiceType         : Win32OwnProcess, InteractiveProcess

Get-Service returns Service Objects which have a lot of properties. If I only want certain ones, I'd use it like so:

 C:\git\Core> gsv Spooler | Select Name,Status

Name                : Spooler
Status              : Running

You're using the cmdlet and probably discarding the columns which have the values you need. Run your one-liner again and remove the Select-Object cmdlet to see all of the columns availble, till you find the one that pertains to the Office.

Upvotes: 0

Related Questions