Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created April 13, 2024 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HauptJ/1235e56db96c8167dbed4ec5cbc99659 to your computer and use it in GitHub Desktop.
Save HauptJ/1235e56db96c8167dbed4ec5cbc99659 to your computer and use it in GitHub Desktop.
PowerShell Script to Count Occurrences of Regular Expressions in a File
# PowerShell Script to Count Occurrences of Regular Expressions in a File
# Specify the path to the file
$filePath = "C:\Users\redacted\test.txt"
# Define the list of regular expressions to search for
$regexPatterns = @("\bwinget\b", "\bmsstore\b", "\bUnknown\b")
# Check if the file exists
if (Test-Path $filePath) {
# Loop through each regex pattern
foreach ($pattern in $regexPatterns) {
# Count occurrences of the regex pattern in the file
$count = (Select-String -Path $filePath -Pattern $pattern -AllMatches).Matches.Count
# Output the count
Write-Output "The regex pattern '$pattern' occurs $count times in the file."
}
} else {
# Output a warning if the file does not exist
Write-Output "The specified file does not exist: $filePath"
}
@HauptJ
Copy link
Author

HauptJ commented Apr 13, 2024

powershell -ep Bypass C:\Users\redacted\regex_count_example.ps1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment