syntax error

Roger Roger 4,956 Reputation points
2022-01-30T20:55:52.443+00:00

Hi All

I am using the below query to pull all the users from the country USA, in the below query i want to exclude users from one particular OU which is Temp OU(OU=TempOU,OU=MyOU,DC=mycompany,DC=com). how do i exclude TempOU in the below query
OU=TempOU,OU=MyOU,DC=mycompany,DC=com

$Syntax = "(co -like '*USA*')"
Get-ADUser -Filter $Syntax -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname |Select DisplayName,SamAccountName,EmailAddress,Userprincipalname Export-csv C:\temp\output.csv -Notypeinformation
Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,507 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,400 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,994 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,406 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2022-01-30T22:13:02.24+00:00

    There's no provision to exclude OU's in a filter (or to include them, either), but you can exclude them from the results.

    $Syntax = "(co -like '*USA*')"
    Get-ADUser -Filter $Syntax -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname |
        Where-Object {$_.distinguishedName -notlike "*,OU=TempOU,OU=MYOU,DC=*"} |
            Select-Object DisplayName,SamAccountName,EmailAddress,Userprincipalname|
                Export-csv C:\temp\output.csv -Notypeinformation
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Roger Roger 4,956 Reputation points
    2022-01-31T10:17:26.98+00:00

    How can i add two OUS or For example i have a TempOU and it has sub OUS, how can i exclude all OUS under Temp OU

    OU=TempOU,OU=MyOU,DC=mycompany,DC=com
    OU=TempOU2,OU=TempOU,OU=MyOU,DC=mycompany,DC=com