How can I check for the existence of an organizational unit without using the [adsi]::Exists()
method? I can't for the life of me get it work on my system.
Asked
Active
Viewed 1,326 times
-1

Ansgar Wiechers
- 193,178
- 25
- 254
- 328

user8056359
- 437
- 1
- 7
- 16
-
Have you tried `Get-ADOrganizationUnit`? – Maximilian Burszley Oct 23 '17 at 19:35
-
I've tried and struggled with it. The documentation tells me little of use. It's too technically written, it assumes that you already understand it. – user8056359 Oct 23 '17 at 19:37
-
[This article](https://technet.microsoft.com/en-us/library/ee617236.aspx) gives nice examples of its use. `If (Get-ADOrganizationalUnit -Identity 'OU=Europe,CN=Users,DC=corp,DC=contoso,DC=com') {'It exists!'}` – Maximilian Burszley Oct 23 '17 at 19:45
-
What does "CN=Users" mean? – user8056359 Oct 23 '17 at 20:45
-
[Here's a good breakdown for you](https://technet.microsoft.com/en-us/library/cc977992.aspx) – Maximilian Burszley Oct 23 '17 at 20:47
-
The problem here is that when it exists, it moves into the if-statement, but when it doesn't it spits out a nasty error. – user8056359 Oct 23 '17 at 20:59
1 Answers
0
The Exists()
method requires an LDAP URI as its argument:
$ou = 'ou=foo,dc=example,dc=com'
[adsi]::Exists("LDAP://$ou")
If you want to use the Get-ADOrganizationalUnit
cmdlet instead: use the -Filter
parameter rather than the -Identity
parameter. The former is usually more forgiving with AD cmdlets:
$ou = 'ou=foo,dc=example,dc=com'
Get-ADOrganizationalUnit -Filter "distinguishedName -eq '$ou'"

Ansgar Wiechers
- 193,178
- 25
- 254
- 328