I have aproblem with this Powershell snippet. The problem is that when @search_string is one single string, it finds it in the file, BUT when @search_string contains as space, it doesn’t find it. e.g. if @search_string = “music”, it finds it in the documents but my problem is, if @search_string = “rock music”, “rock music” is not found in the documents even though it is in there. My snippet
# Prompt user for the search string
$search_string = Read-Host "Enter the search string:"
# Initialize a list to store filenames where the search string was found
$found_filenames = @()
# Search each file in the "wordXMLs" folder
foreach ($xml_file in Get-ChildItem -Path $wordXMLsFolder -Filter "*.xml") {
$xml_content = Get-Content -Path $xml_file.FullName -Raw
# Perform case-insensitive and fuzzy match search
if ($xml_content -match [regex]::Escape($search_string)) {
$found_filenames += $xml_file.Name
}
}
What I have tried:
-
The doco for Read-Host – but it seems to not have an answer to why this $search_string behaves this way.
-
Enclosing my user input in double quotes and single quotes – still not found
-
Debug code within the snippet
Write-Host “$search_string_input = ‘”,$search_string_input,”‘”
—> when I supply the value the music with no quotes, I get the unexpected value of “‘ the music ‘”. I expected ‘the music’
- CoPilot – pffft!