Introduction
As I was research one of my blog posts for February, I ran across some older articles and cheatsheets on PowerShell some of which displayed interesting uses of foreach loops and data matching. The more interesting one that I saw was taking a data set of some sort and then searching for a subsection of that data via either RegEx or via a string using the Like method. For example, if we have a data set with a list of files or directories and we could use these techniques to find those that start with C or D or some other character that we desire.
First, let’s break down the two methods and then apply this to a real world example for further understanding.
RegEx Match
With this method we will use ReEgEx to match just the first letter. So if we wanted to match a first letter of ‘d’, the RegEx would be ‘^d’ and to use this with PowerShell we can do so like this:
Get-ChildItem | Where Name -Match '^d'
Resulting in these displayed results:
String Match
Using this method, we would again look for the first character to be ‘d’ and use the wildcard ‘*’ together to make sure any file or directory starting with ‘d’ would be chosen:
Get-ChildItem | Where Name -Like 'd*'
Same result as the RegEx methodology. Your search criteria will determine which methodology is better with RexEx providing more options in pattern matching.
Previous RegEx Articles
Real World Example
Now that we’ve reviewed the background, I wanted to go over the code for a script that will break up the list of PowerShell Aliases and in doing so we will sort the aliases alphabetically. Visually, we will also note each letter of the alias. With each alias, it’s corresponding PowerShell cmdlet is also displayed for reference.
First, we have the RegEx example.
RegEx Example
$Letters = 'a', 'b', 'c', 'd', 'e' , 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' Foreach ($Letter in $Letters) { $RegEx = "^"+"$Letter" Write-Host "Aliases - $Letter" -ForegroundColor Green Get-Alias | % {if ($_.name -match $RegEx) {Write-Host $_.name ' stands for: ' $_.definition}} }
The results would look like this:
Next, we can instead use the String Match method:
String Match Example
$Letters = 'a', 'b', 'c', 'd', 'e' , 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' Foreach ($Letter in $Letters) { $String = "$Letter"+'*' Write-Host "Aliases - $Letter" -ForegroundColor Green Get-Alias | % {if ($_.name -like $String) {Write-Host $_.name ' stands for: ' $_.definition}} }
Article Links
Here are the articles which drove me to this code / solution / inspiration of an article:
Article one
https://powershellmagazine.com/2014/10/22/foreach-and-where-magic-methods/
Which lead me to Article Two:
https://www.theochem.ru.nl/~pwormer/teachmat/PS_cheat_sheet.html#TOC14 –> See #10
RegEx Resources
https://regex101.com/
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.3
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Conclusion
As this is a Quick PowerShell post, the depth of the article is shallow and intended to point you to ideas or concepts that are new to you. Also included in the article are additional article links and resources to help you expand upon this base.
Comments? Questions?
Feel free to leave your Comments below! Learn to more efficiently utilize PowerShell to manage Exchange Server, Exchange Online, Microsoft Defender for Office or Microsoft Purview Compliance portals by picking up frequently updated eBooks: