Examples of using Replacement Patterns and Character Escapes when using -remove

Hi all, I’m struggling to understand these when running -replace on a string in PowerShell.
I found these two articles that define them, and but the examples are .NET and not PowerShell:

My question is: can someone point me towards a page detailing basic examples of using these to process text in PowerShell?

This time what I was trying to do is filter AD group names out of a string of DN’s, some under OU’s and others under CN’s. This I what I cobbled together to get the data that I wanted:

$justavi = Get-ADPrincipalGroupMembership -Identity "nora" | Select-String avionics
$delimiter1 = $justavi -replace "CN=",""
$delimiter2 = $delimiter1 -replace "(,Users,).*$",""
$delimiter3 = $delimiter2 -replace "(,OU=).*$",""
$delimiter3

While this did get me the list of AD groups, I’d like to understand how .*$ works and how to use the other escape and substitution options to do things like this more efficiently in the future. Thank you!

This is the wrong approach. In PowerShell we have the advantage to be able to work with rich and powerful objects instead of stupid boring strings.

If we want to filter the output of a PowerShell cmdlet we would use Where-Object - not Select-String.

In what property do you expect the string avionics to be? Or in other words - what do you actually want to achieve?

Edit:

If you you want to extract the names of the groups from their distinguished names you actually don’t need regex acrobatics at all. :wink: Most of the time that’s even easier and a little more reliable and less error prone.

With

$GroupList = Get-ADPrincipalGroupMembership -Identity 'nora'

you get all groups the user nora belongs to.

And just to show what I mean we pick only the first group to play around with:

$Group = $GroupList[0]

Assumed you dont have commas in the names of your groups we can split the distinguished name of the group by the commas:

$SplittedDN = $Group -split ','

And since we’re actually only interested in the first part we pick it the same way we picked the first group:

$Name = $SplittedDN[0] 

But we still have the CN in front of the name we want.
So we split again - this time by the = sign. And since we alread know that we need the seccond element of the resulting array we can combine the splitting and the index pick in one step like this:

($Name -split '=')[1]

Was it that you was looking for?

1 Like

Thanks for the guidance, Olaf! In the future I’ll try to approach tasks like this with an object-based frame of mind rather than a string-based one :slight_smile:

Yep, the simple output of the AD group name is exactly what I was hoping for. Would it be safe to assume that to get all the AD groups that I belong to with avionics in the same is a simple foreach away? Or would you do that in another way?

You don’t need a loop for that

$GroupList =
Get-ADPrincipalGroupMembership -Identity 'nora' |
    Where-Object -Property DistinguisehdName -Match -Value 'avionics'
$GroupList

Since you’re a beginner I’d recommend to describe what you’re actually looking for - not how to achieve it the way you think you need to got to get what you’re looking for. :wink:

Oh man, that’s so easy!!!

And yeah, I tend to overcomplicate things in my head. My object-based pondering of issues such as this begins TODAY! :slight_smile:

:smiley: :+1:t4: :love_you_gesture:t4:

As a reference for future me or others like me, this is the code I ended up using:

Get-ADPrincipalGroupMembership -Identity "nora" | Select-Object -ExpandProperty Name | Where-Object { $_ -like "*avi*" } 

For better readability you should add line breaks after each pipe symbol.

Here you can read more about