Prevent Variable Expansion?

Welcome Forums General PowerShell Q&A Prevent Variable Expansion?

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

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #73424

    Participant
    Topics: 3
    Replies: 6
    Points: -18
    Rank: Member

    Hi,

    Is there any way to stop variable expansion happening with parameter values that are passed to a function?

    For example, if I run the below function and neglect to use single quotes for the parameter value, the double dollar automatic variable is expanded:

    function Test-Parameter {
        param(
            [string]$String
        )
        $PSBoundParameters
    }
    
    Test-Parameter -String stringwithdollars$$
    

    I realise this is just 'how PowerShell works' but wondered if there is anyway to foolproof this.

  • #73457
    js

    Participant
    Topics: 21
    Replies: 592
    Points: 1,149
    Helping Hand
    Rank: Community Hero
    Test-Parameter -String 'stringwithdollars$$'
    • #73460

      Participant
      Topics: 3
      Replies: 6
      Points: -18
      Rank: Member

      Yeah I get that. Just wondering if there's a way to stop things messing up if someone gets lazy with the quotes or mistakenly uses double quotes.

The topic ‘Prevent Variable Expansion?’ is closed to new replies.

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