How to add quotes in an argument when passing to a function in Powershell

Viewed 360

I need to add '"$third'" these quotes to a argument to get the right result. Below is the code i have

$first = Function-xyz $second $third

Here somehow if we can add '"$third"' and pass the argument it will work for me, but can do that with below approach

$first = Function-xyz $second '"$third"'

With this it just pass third itself as a string without reading its value.

1 Answers

Welcome.

Here's how I would do it:

$third = "Some Text"
Return '"{0}"' -f $third

at the end of your function. This will expand the value of Third: "Some text"

If you would do this:

$Third = '$Second'
Return '"{0}"' -f $third

It would result in: "$Second"

If you would do this:

$Second = 2
$Third = "$Second"
Return '"{0}"' -f $third

It would result in: 2

Related