PowerShell for Windows Admins

Jul 4 2018   5:34AM GMT

Variable squeezing

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

When you’re developing code interactively how often do you set a variable and then display it to test the result? You can do that in one go using variable squeezing.

Normally you’d do:

PS> $test = 1
PS> $test
1

Set the variable and display the contents as two separate actions.

By enclosing the first statement in parentheses you accomplish both tasks

PS> ($test2 = 2)
2

Great for interactive work but you don’t want in in scripts as you’d be getting output every time you set a variable value!

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: