String variable as argument to Get-ChildItem

11,658

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 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.

Share:
11,658
tor
Author by

tor

Student in Computer Science.

Updated on June 15, 2022

Comments

  • tor
    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?