Created
April 13, 2024 22:55
-
-
Save HauptJ/1235e56db96c8167dbed4ec5cbc99659 to your computer and use it in GitHub Desktop.
PowerShell Script to Count Occurrences of Regular Expressions in a File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
powershell -ep Bypass C:\Users\redacted\regex_count_example.ps1