Search code examples
regexpowershell

PowerShell Regex: Removing specific parts of a string based on patterns


I want to remove all parts of the string that I don't need, leaving only the name. I’m not getting the expected result. Instead of just "Aryshenskyi, Mihail", I’m getting an empty string. What am I doing wrong, and how can I modify my code to achieve the desired output?

$text = "CN=Aryshenskyi\, Mihail,OU=Users,OU=SMZ,DC=smzmi,DC=local"
$regex = "(CN=).+?(\\), .+?(,OU=.+)"
$result = $text -replace $regex, ""
Write-Host $result

Solution

  • You can adapt the regexes found in these answers with -replace to extract the common name then you can apply another replace for the backslash. See https://regex101.com/r/Clg5xj/1 for regex details.

    $text = "CN=Aryshenskyi\, Mihail,OU=Users,OU=SMZ,DC=smzmi,DC=local"
    $text -replace '^CN=|(?<!\\),.+' -replace '\\'
    

    You could adapt the regex you currently have to make it work in a single replacement however this one only works for Distinguished Names having an escaped comma in their common name. It would also fail for users in a Container instead of in an OU. See https://regex101.com/r/46Xa2V/1 for regex details.

    $text = "CN=Aryshenskyi\, Mihail,OU=Users,OU=SMZ,DC=smzmi,DC=local"
    $text -replace '^CN=(.+?)(?:\\)(, .+?),OU=.+', '$1$2'
    

    Adding another option that should handle the replacement on one go including removing the backslash escaping the comma in the common name. See https://regex101.com/r/YuGge3/1 for regex details.

    $dns = @(
        'CN=Aryshenskyi\, Mihail,OU=Users,OU=SMZ,DC=smzmi,DC=local'
        'CN=Aryshenskyi Mihail,OU=Users,OU=SMZ,DC=smzmi,DC=local'
    )
    
    $dns -replace '^CN=|\\(?=,)|(?<!\\),.+'