syntax error

Glenn Maxwell 10,781 Reputation points
2022-04-04T20:18:47.217+00:00

Hi All

i want to export all users whose Titles or descriptions can be anything except AB AC, BA, BC. i am using the below query but i am not getting output.

$A ="(title -like '*A*' -and title -notlike '*B*' -and title -notlike '*C*' -and (title -like '*D*' -or title -like '*E*' -or title -like '*F*') -or (description -like '*A*' -and description -notlike '*B*' -and description -notlike '*C*' -and (description -like '*D*' -or description -like '*E*' -or description -like '*F*')"
Get-ADUser -Filter $A -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname| Export-csv C:\temp\list.csv -Notypeinformation
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,635 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,244 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,906 Reputation points
    2022-04-04T21:32:03.667+00:00

    What's wrong with something simple, like:

    $A = "
            (
                title -ne 'AB' -and
                title -ne 'AC'
            ) -or
            (
                description -ne 'AB' -and
                description -ne 'AC'
            )
        "
    

    If you'd rather go for something more complex:

    $A ="
        (
            title -like '*A*' -and 
                title -notlike '*B*' -and 
                    title -notlike '*C*' -and 
                    (
                        title -like '*D*' -or 
                        title -like '*E*' -or 
                        title -like '*F*'
                    )
        ) -or 
        (
            description -like '*A*' -and 
                description -notlike '*B*' -and 
                    description -notlike '*C*' -and 
                    (
                        description -like '*D*' -or 
                        description -like '*E*' -or 
                        description -like '*F*'
                    )
        )"
    

    I think you're introducing ambiguity with the broad use of wildcard matches.

    0 comments No comments

0 additional answers

Sort by: Most helpful