I suddenly can’t seem to split a string, which I’ve done successfully before. The string is a list of exclusions for Get-ChildItem, which I want to display in a more readable format so the user can easily verify they’re correct.
I’m using PowerShell 7.3.4. The code:
# Regular expression of files/folders to exclude from the search
$exclusions = "\.bak| - Copy|\.db|\.ini|\.log|\.temp|template|test|\.tmp|New-Folder|prototype|_vti"
$substrings = $exclusions -split "|"
$substrings
$count = $substrings.count
Write-Host "Number of exclusions: $count"
foreach ($string in $substrings) {
Write-Host "► $_"
}
The only output correctly says “Number of exclusions: 12,” but never shows them.
The backslashes tell the regular expression parser to treat the periods literally. Could the backslashes be interfering with the -Split call?