Reputation: 9854
I have two very similar one line commands but one works and one doesn't with variables.
Copy-Item "C:\BadSourceCodes\dropTest\base1\*" -Include "myUsers.Config","myUsers.WSF" -Destination "C:\BadSourceCodes\dropTest\temp1"
This just works I have two files and I want them to be copied over to destination folder.
If I create something like this
$files = "myUsers.Config,myUsers.WSF"
$tempFiles = [string]::Concat("`"",$files.Replace(",","`",`""),"`"")
$tempFiles
Copy-Item "C:\BadSourceCodes\dropTest\base1\*" -Include $tempFiles -Destination "C:\BadSourceCodes\dropTest\temp1" -Force
As soon as I use a variable for Include files it doesn't work. I have checked with different variations and it doesn't work.
Is the $ variable behaves differently?
I tried following variations like $($tempFiles), '$tempFiles', "$tempFiles".....
UPDATE: I think when I am escaping the quotes and replacing , with "," then it treats quote as a part of the string. Some how it was the bad approach I took. But what worked is just $files.Split(",") which creates an array of string and Done.
Upvotes: 0
Views: 757
Reputation: 29460
The -Include
argument wants an array of strings, not a list of names in a single string. Put the names in an actual array and it should work:
#create an array of strings
$files = "myUsers.Config","myUsers.WSF"
Copy-Item "C:\BadSourceCodes\dropTest\base1\*" -Include $files -Destination "C:\BadSourceCodes\dropTest\temp1" -Force
Upvotes: 3