Senior Systems Engineer
Senior Systems Engineer

Reputation: 1139

' Error Message: 'Operator Not supported: -notcontain' in Get-ADUser Filter cmdlet?

Unable to get the AD users created in the past 1 year which is not contains the specific Domain name pattern:

$laterThan = (Get-Date).AddYears(-1)
$filter = { (whenCreated -gt $laterThan) -and (userPrincipalName -notcontain $((Get-ADDomain).Name)) }
$properties = 'canonicalName', 'whenCreated', 'displayName', 'lastlogondate'

Get-ADUser -Filter $filter -Properties $properties

Error showing as:

Get-ADUser : Error parsing query: ' (whenCreated -gt $laterThan) -and (userPrincipalName -notcontain $((Get-ADDomain).Name)) ' Error Message: 'Operator Not supported: -notcontain' at position: '55'. At line:5 char:1

  • Get-ADUser -Filter $filter -Properties $properties
  •   + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
      + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
    

However, the IDE not showing any issue: enter image description here

Upvotes: 2

Views: 1175

Answers (2)

marsze
marsze

Reputation: 17055

You probably mean -notcontains but that operator is not supported for that cmdlet. Look here.

Also, that operator works with collections, not strings. If you want to check if a string contains another string, use the -like operator and wildcards:

Get-ADUser -Filter "UserPrincipalName -like '*$((Get-ADDomain).Name)*'"

Upvotes: 2

JoeBigToe
JoeBigToe

Reputation: 1021

That's because the correct operator is -notcontains

Check them all here.

Upvotes: 0

Related Questions