how to use variable in path

Welcome Forums General PowerShell Q&A how to use variable in path

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 year, 8 months ago.

  • Author
    Posts
  • #66313

    Participant
    Points: 0
    Rank: Member

    Hi,

    Bit new to powershell & trying to append a variable to windows path.
    Say my variable is $filename (whose value is abc.txt) & i want to append it to my C drive like C:\$filename

    Wondering what is the correct syntax for this ?

    Thanks in advance for the help !

  • #66316

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    Use Join-Path.

  • #66343

    Participant
    Points: 0
    Rank: Member

    Join-Path, as Don mentions, can be very robust for using wildcards in paths to resolve multiple paths, but for what you are looking at, I find it simpler just to use the variable in a double-quoted string.

    $filename = "abc.txt"
    "C:\$filename"

    In the above $filename would be replaced with the value of the variable and the result would be "C:\abc.txt"

The topic ‘how to use variable in path’ is closed to new replies.