Friends,
I am trying to write a script that will get the whole DistinguishedName
When
The one OU is “ABCDEF” And
Some, another OU is “Navy”
There will be only one DistinguishedName that has both.
This one-liner works to find all the ABCDEF:
Get-ADOrganizationalUnit -Filter 'Name -like "ABCDEF"' | Format-Table Name, DistinguishedName
Name DistinguishedName
---- -----------------
ABCDEF OU=ABCDEF,OU=Users,OU=Submarine,OU=Ship,OU=Navy,OU=Agencies,DC=usa,DC=net
ABCDEF OU=ABCDEF,Destroyers,OU=Ships,OU=Agencies,DC=usa,DC=Net
ABCDEF OU=ABCDEF,OU=Officers,OU=People,OU=Agencies,DC=usa,DC=net
ABCDEF OU=ABCDEF,OU=Users,Snipers,OU=Recon,OU=Marines,OU=Agencies,DC=usa,DC=net
ABCDEF OU=ABCDEF,OU=Officers,OU=Recon,OU=Marines,OU=Agencies,DC=usa,DC=net
ABCDEF OU=ABCDEF,OU=Users,OU=Cooks,OU=Engineerr,OU=Seabees,OU=Agencies,DC=usa,DC=net
ABCDEF OU=ABCDEF,OU=Department,OU=Users OU=Organization,Army,OU=Agencies,DC=usa,DC=net
ABCDEF OU=ABCDEF,OU=Users,OU=Users OU=Army,OU=Agencies,DC=usa,DC=net
But I do not want all of them.
I want only one. I want the one that has both, “ABCDEF” and “Navy”
I tried:
$DName = (Get-ADOrganizationalUnit -Filter 'Name -like "*ABCDEF*" -and Name -like "*Navy*"' -Properties distinguishedName).DistinguishedName
I expected to get this: OU=ABCDEF,OU=Users,OU=Submarine,OU=Ship,OU=Navy,OU=Agencies,DC=usa,DC=net
But of course there is no name that is both “ABCDEF” and “Navy” at the same time.
And so, what would give me only this result?
OU=ABCDEF,OU=Users,OU=Submarine,OU=Ship,OU=Navy,OU=Agencies,DC=usa,DC=net
Thank you in advance for your help.