Escaping a double quote in Get-ADOrganizationalUnit

Hopefully this is something stupid simple. I have been struggling with a customer that wants to rebuild their A.D. OU structure in several domains. They have hundreds of department IDs they want pulled from PeopleSoft, and they would like these used for the OU names (e.g. an eventual OU of 18639 - Account Managers)

The problem is their naming scheme is all over the place for IDs in PS. Names have just about every special character imaginable in them. The customer would also like the names to look exactly like they do in PeopleSoft, so no stripping. New-ADOrganization unit works just fine, I can escape special characters and create the containers just fine.

The problem comes in where I am pulling in a list of OUs, comparing with a list obtained from PeopleSoft, and creating any new OUs. While I can escape every other special character, I am unable to find a way to escape a double quote. So while New-ADOrganizationalUnit will create something like 45009 - Kitchen “Donut Shop” just fine, Get-ADOrganizationalUnit cannot find it, so it tries to recreate it. Then it fails on creation of course, as it already exists.

I’d say it depends on how you try to tell Get-ADOrganizationalUnit to find it. :wink: So how do you tell it to find it?

Have you tried to search for a solution first before you came here to ask for help? Esacping strings with special charachters is one of the most asked questions for PowerShell. There are literally thousands of examples out there. Not just here on PowerShell.Org. … and here
Escape characters - PowerShell -
… and here …
What is the literal escape character in Powershell? - Server Fault
… and here …
powershell 2.0 - Escaping quotes and double quotes - Stack Overflow

You may share the code you’ve run into issues with so we might be able to help you further more specifically.

Thanks for the response. I thought I had stated that I had already tried the normal escape characters, and that it worked for everything except when trying to filter both single and double quotes. Perhaps I didn’t explain well enough. In the end I figure I will just have to do a secondary replace:

$OU = "OU=Departments,DC=my,DC=company"

$aStructure = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter *  |
    Select Name
 & {foreach ($container in $aStructure) {
    ($container.Name -replace "[/,'()]", "").Replace('"', '')
   }} | Out-GridView

Will successfully filter out all special characters I am after

Great that you found a solution yourself and thanks for sharing. :wink: :+1:t4:

Just a thought, take it as you wish. I moderate a number of other language forums (PS is a secondary language for me), and have seen other previously helpful people -have even been guilty of it myself - get frustrated over time and begin helping less and berating people more for not doing what they feel is the proper amount of research.

Those who ask questions without research are always going to be there; you are either correct and they are lazy (it happens) or they are new and don’t KNOW what they don’t know, so searching is difficult. In the forums I admin, we suggest people do one of two things: help anyway, remembering that we were all on Day 1 once, or ignore the thread. To do anything else generally leads to a less than welcoming experience for the users.

1 Like

Having a play around with this and I found that an LDAP Filter works:

Get-ADOrganizatinalUnit -LDAPFilter '(Name=45009 - Kitchen "Donut Shop")'
1 Like

Thanks, that works. It is so weird: when I run it using -LDAPFilter, I get this for the distinguishedName:

DistinguishedName: OU=45009 - Kitchen, “Donut Shop”,OU=Departments,DC=my,DC=company,DC=com

However if I just use the -Identity, and the same escape characters, it fails:

Get-ADOrganizationalUnit -Identity 'OU=45009 - Kitchen\, \"Donut Shop\",OU=Departments,DC=my,DC=company,DC=com'

Thanks for the nudge, another way to do it.

That seems to be a slightly different name to what you had before, you’ve introduced an escaped comma after kitchen. This worked for me:

Get-ADOrganizationalUnit -Identity 'OU=45009 - Kitchen \"Donut Shop\",DC=ad,DC=contoso,DC=com'

Yeah, the actual name is 45009 - Kitchen, “Donut Shop”. I think I omitted the comma earlier in the post.

I just created an OU with a comma after kitchen and it worked for me:

Get-ADOrganizationalUnit -Identity 'OU=45009 - Kitchen\, \"Donut Shop\",OU=Departments,DC=ad,DC=contoso,DC=com'