M
M
MODifikaTOR2021-02-11 16:02:48
PowerShell
MODifikaTOR, 2021-02-11 16:02:48

How to exclude certain OUs by CanonicalName from a query?

I'm trying to build a query in AD that will give me usernames and their path (CN and CanonicalName) excluding accounts located in two OUs. However, Powershell gives me an error:
60252705866bc723165673.png
The code itself:

Get-ADUser -Filter "(CanonicalName -notlike '*off_user*') -or (CanonicalName -notlike '*service*')" -Properties CN,CanonicalName | Format-Table CN,CanonicalName -AutoSize

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Bezrukov, 2021-02-11
@MODifikaTOR18

CanonicalName is a calculated attribute (constructed attribute), such attributes are not stored explicitly in AD, but are calculated on the fly when appropriate requests are received. Accordingly, they cannot be directly used in filters and queries.
Your request should look something like this:
Get-ADUser -Filter "(saMAccountName -notlike '*off_user*') -or (saMAccountName -notlike '*service*')" -Properties CN,CanonicalName | where{$_.distinguishedname -notmatch "exOU1|exOU2|exOU3"} | ft CN,CanonicalName -auto

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question