Regular Expressions are a -replace's best friend
Are you familiar with PowerShell’s -replace operator?
"John Jones" -replace "Jones","Smith" Most folks are aware of it, and rely on it for straightforward string replacements like this one. But not very many people know that -replace also does some amazing stuff using regular expressions.
"192.168.15.12,192.168.22.8" -replace "\.\d{2}\.","10" That’d change the input string to “192.168.10.12,192.168.10.8,” replacing all occurrences of two digits, between periods, to 10. The 12 would be skipped because it isn’t followed by a period, as specified in the pattern. Note that all occurrences are replaced, in keeping with the usual operation of -replace.
The operator can also do capturing expressions, and this is where it gets really neat-o.
"Don Jones" -replace "([a-z]+)\s([a-z]+)",'$2, $1' Here, I’ve specified two capturing expressions in parentheses, with a space character between them. PowerShell will capture the first to $1, and the second to $2. Those aren’t actually variables, which is important. In my replacement string, I put $2 first, followed by a comma, a space, and $1. The resulting string will be “Jones, Don”. It’s important that my replacement string be in single quotes. In double quotes, the shell will try and treat $1 and $2 as variables, instead of using them as captured regex placeholders. I kinda wish they’d used something other than a $ for the captured placeholders, so that they didn’t look like variables, but the syntax is in keeping with regex standards.
I think it’s cool to see all the places a regex can be put to use. The -split operator also supports regex syntax as a way of specifying the separator that will be used to break a string into components, so you’re not limited to splitting just on a single character like a comma.
Apart from the well-known -match operator and the Select-String command, where else have you used a regex in PowerShell?
Related Articles
So you want to start a User Group
But where do you begin? I’ve blogged about this from the reversed perspective on my own blog about finding user groups with a small section about what you can do if your thinking about getting one off the ground which you can read at http://blog.kilasuit.org/2016/04/17/how-to-find-local-user-groups-events-my-experience/ and it was only natural to eventually blog from the other side too although this has come up a bit earlier than I had planned to but alas it gets it done As the Coordinator for the UK PowerShell User Groups I learned a few things the hard way with setting up a user group and here are just a few things that you will need to get sorted first which will hopefully help you on your way.
ICYMI: PowerShell Week of 02-April-2021
Topics include help sections, Approved Verbs, Identity Management and more…
Media Sync: Organize Your Photos and Videos with PowerShell
Do you have photos and videos that you have taken over the years that are scattered all over the place? Do you want to have all your photos and videos organized? Do you want all your photos and videos to have a standardized naming scheme? If you answered YES to these questions, then this is the post for you. In this post, I will provide you with the PowerShell code and examples for how to use the Media Sync script.