Search code examples
arrayspowershellget-childitem

Powershell -include $variable trouble


Why doesn't $arraychoices work in this gci?.. But if I hard code the file type extension in it does?

Doesn't work

$filetypes = @()
$filetypes += '*.pdf'
$filetypes += '*.txt'
$ftc = $filetypes
$arraychoices = "('$($ftc -join "','")')"

$fr = Get-ChildItem C:\backuptest **-include $arraychoices** -Recurse 
$files = $fr.fullname
$files

Harcode Works?

Get-ChildItem C:\backuptest **-include ('*.pdf','*.txt')** -Recurse 

What am I missing? Im my above example. Im using check boxes for file type extensions, so the hard code method isn't an option.

Any help is appreciated. TIA


Solution

  • $filetypes is already an array of strings - pass it directly to the -Include parameter:

    $filetypes = @()
    $filetypes += '*.pdf'
    $filetypes += '*.txt'
    
    Get-ChildItem -Include $filetypes ...