Copy-Item -Include with variable

Hi Guys,

I have the following code that works fine:

[pre] $NetPath = “J:\DB\MyFolder*”

$TmpPath = $env:APPDATA + “\TmpFolder”

Copy-Item -Include *.exe, *.rpt, *.chm -Path $NetPath -Recurse -Force -Destination $TmpPath

[/pre]

Now I want to pass the values for -Include in a variable. I do this as follows:

[pre]

$Include = “*.exe, *.rpt, *.chm”

Copy-Item -Include $Include -Path $NetPath -Recurse -Force -Destination $TmpPath

[/pre]

The data will not be copied. What is wrong?

 

If you look at the documentation, Include is a string array:

Get-Help Get-ChildItem

The variable is defined as string, so you need to make it an array like it’s being passed in the first command:

PS C:\Users\rasim> $Include = "*.exe, *.rpt, *.chm"

PS C:\Users\rasim> $Include
*.exe, *.rpt, *.chm

PS C:\Users\rasim> $Include = '*.exe', '*.rpt', '*.chm'

PS C:\Users\rasim> $Include
*.exe
*.rpt
*.chm

It works very well. Thank you for your help