using a verable in a string with out a space

Welcome Forums General PowerShell Q&A using a verable in a string with out a space

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

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #33004

    Participant
    Points: 0
    Rank: Member

    This one should be easy and I should be able to find it online but I must not be asking the google machine the right question.
    I need to use a verable as part of a file name nested in another verable.
    Simply put I need to set a verable that points to a file name. And that file name I need to pull its verable from a param passed via the cmd line.
    Looks something like this

    Param([Parameter(Mandatory=$true)][String]$a1
    $Filename ="D:\Folder\$a1 File.txt"
    

    I need to remove the spacein the file name.

    As always Thanks!!!!

  • #33007

    Member
    Points: 0
    Rank: Member

    You can place the variable name in curly braces to help the parser figure out which part is the variable, and which is the string after it:

    "${a1}File.txt"
    

    You can also use the subexpression operator, but I prefer the first option instead. You can see why when you look at this code in the ISE; the syntax highlighting is nice and neat with the braced variable names.

    "$($a1)File.txt"
    
  • #33008

    Participant
    Points: 0
    Rank: Member

    Perfact!!!

    Thanks 🙂

The topic ‘using a verable in a string with out a space’ is closed to new replies.