passing varaibles to function

Welcome Forums General PowerShell Q&A passing varaibles to function

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

 
Member
5 years, 3 months ago.

  • Author
    Posts
  • #11344

    Participant
    Points: 0
    Rank: Member

    is the only way to pass variables to function is like this :

    function test {
    params([string]$arg1)
    Write-Host $arg1;
    }
    i understand i can't call
    test "dddd"+"eeeee"
    but only
    $str="dddd"+"eeeee"
    test $str

    isn't there another way to concat parms on the line i call the function?

  • #11348

    Participant
    Points: 0
    Rank: Member

    Your code has a type – it should be param not params

    Its not the only way but I think its the best especially when you start to look at advanced functions

    you could do

    test ("dddd"+"eeeee")

  • #11349

    Participant
    Points: 0
    Rank: Member

    OK

  • #11350

    Member
    Points: 0
    Rank: Member

    Yep, put parentheses around the expression to force PowerShell into Expression mode parsing, instead of Argument mode. See the about_Parsing help file for all the details on this.

The topic ‘passing varaibles to function’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort