Reputation: 636
Why is it that this works:
PS C:\Users\user> Get-ChildItem '\\COMPUTER\folder with spaces'
But this does not:
PS C:\Users\user> $a = "'\\COMPUTER\folder with spaces'"
PS C:\Users\user> Get-ChildItem $a
Cannot find path 'C:\Users\user\'\COMPUTER\folder with spaces''
because it does not exist
How can I use a variable to do something to the effect of the latter?
Upvotes: 2
Views: 13290
Reputation: 200293
Nested quotes become part of the string.
Get-ChildItem '\\COMPUTER\folder with spaces'
This will read the child items from the share folder with spaces
on the host COMPUTER
.
Get-ChildItem "\\COMPUTER\folder with spaces"
This will do the same, but the double quotes also allow you to use variables inside the string, e.g. like this: "\\$computer\folder with spaces"
.
Get-ChildItem "'\\COMPUTER\folder with spaces'"
This, however, will look for a file or folder with the literal name '\\COMPUTER\folder with spaces'
(including the single quotes) in the current working directory on your local computer.
Upvotes: 3
Reputation: 636
Further experiments revealed this solution:
PS C:\Users\user> $a = "\\COMPUTER\folder with spaces"
PS C:\Users\user> Get-ChildItem "$a"
Upvotes: 4