I’m trying to write a PowerShell script that will allow a user to pass in a parameter with multiple types of files they want to process (i.e. *.txt and *.docx). When I run the Get-Childitem command with the -include option and manually type in the file types, it works file. But when I try to supply a variable for the -include mask, it does not work (returns nothing).
The problem only occurs when I’m using multiple file types $incFiles = “””.txt””`, “”.docx”””
If I use a single file type, it works just fine. $incFiles = “*.txt”
$incFiles = ""
$incFiles = """*.txt""`, ""*.docx"""
Write-Host "Manual Method:" '"*.txt", "*.docx"'
Write-Host "Calcul Method:" $incFiles
Write-Host "`nManual method works"
Get-ChildItem -path "C:\users\paul\Downloads" -Recurse -Include "*.txt", "*.docx"
Write-Host "This does not work"
Get-ChildItem -path "C:\users\paul\Downloads" -Recurse -Include $incFiles
Write-Host "End"
Results
Manual Method: "*.txt", "*.docx"
Calcul Method: "*.txt", "*.docx"
Manual method works
Directory: C:\users\paul\Downloads\Test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/28/2020 9:54 AM 0 File1.txt
-a---- 3/28/2020 9:55 AM 0 File2.docx
This does not work
End