replace string (including escapes)

Welcome Forums General PowerShell Q&A replace string (including escapes)

This topic contains 4 replies, has 2 voices, and was last updated by

3 years, 3 months ago.

  • Author
  • #30784

    Points: 0
    Rank: Member


    I have a variable $memberof containing the following value

    Get-AdPrincipalGroupMembership -Identity "user"

    Heres my code
    $memberof | foreach {
    $dn_no_cn = $_.DistinguishedName -replace 'CN=',"
    $groupname = $ + ","
    $ou += ($dn_no_cn -replace $groupname,")

    So what Im trying to do is to get the Parrent OU of the Group in the DN format.

    First: Any suggestions how I can shorten the 3 replace lines into a oneliner?

    Second: the above loop works well except for one Group which contains characters ( and ). Is there a way how I can escape them so the replace operation doesnt fail with them?

    I appreciate your help.

  • #30788

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    You probably -could- shorten in, but I'm not sure there's an advantage in doing so.

    I'm not seeing why either -replace would be failing with parentheses. Can you provide an example of what it's doing that you don't like? Without seeing the input data and the incorrect output, it's kind of difficult to imagine what's wrong.

  • #30794

    Points: 0
    Rank: Member

    Ok – it felt like I was doing too much with all these lines.

    I have an some AD Group where the Input is like this:

    The expected replaced output should be "OU=Testgroup_(I.L.E.E.),OU=Projects,DC=company,DC=dmz"

    But in this case I get the full string back. With all other groups I have no such problem.

    Also I did some testing and this is the simplified problem:
    " test(123)" -replace 't(123)', " won't work but "test(123)" -replace 't\(123\)', " will work.
    So I need to figure out how I can escape these brackets.

    Thank you so much for your help Mr. Jones you sir are a true guru 🙂

  • #30797

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    So, maybe this is confusion over how -replace works. It supports a subset of regular expression syntax, which is what you're seeing in your simplified problem. You should be able to use a backslash or a backtick ` to escape special characters to make them literal.

    But that should only have to be done in the first argument passed to -replace:

    $result = $original -replace $search,$replace

    In this case, any special characters in $original wouldn't be treated as "special" and shouldn't need escaping. Only $search should be looking for wildcards and so on, and potentially require escaping.

    However, possibly replacing isn't the right way to go?

    $original = "CN=Testgroup_(I.L.E.E.)_Mitglieder,OU=Testgroup_(I.L.E.E.),OU=Projects,DC=company,DC=dmz"
    $newou = "OU=Projects,DC=company,DC=dmz"
    $pieces = $original -split ","
    $morepieces = $pieces[0] -split "="
    $final = "$($morepieces[1]),$newou"

    Does that follow the logic you're after?

  • #30802

    Points: 0
    Rank: Member

    Yes I am aware that I only need to use the escape characters in the $search variable. The Problem is that i have like 150 Groups and I need to find out the Parent OU of all these groups with the Distinguished Name Property.

    Turns out I was way off track, I will use the get-ADOrganizationalUnit command and will filter after the Group names I have to get the DNs I need. Maybe I will post again if I have any further questions.

    But for now – Thanks for your help!

The topic ‘replace string (including escapes)’ is closed to new replies.