Prevent Variable Expansion?

This topic contains 2 replies, has 2 voices, and was last updated by  Mike 3 months, 4 weeks ago.

  • Author
    Posts
  • #73424

    Mike
    Participant

    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
    Test-Parameter -String 'stringwithdollars$$'
    • #73460

      Mike
      Participant

      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.

You must be logged in to reply to this topic.