String variable as argument to Get-ChildItem
Solution 1
Further experiments revealed this solution:
PS C:\Users\user> $a = "\\COMPUTER\folder with spaces"
PS C:\Users\user> Get-ChildItem "$a"
Solution 2
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 hostCOMPUTER
.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.
Comments
-
tor almost 2 years
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?