In-Depth

Working with Variables in Sapien's PowerShell Studio

This tool makes it easy to create PowerShell GUIs quickly.

PowerShell scripters have a few choices in the editor they can use to write code. Options range from the PowerShell Integrated Script Environment, Visual Studio Code, Visual Studio, Notepad++ and more. Another great option is Sapien's PowerShell Studio. PowerShell Studio is a product solely designed for PowerShell and building GUIs in PowerShell.

One of the features of PowerShell Studio is how it gives the user the ability to work with variables. Because PowerShell Studio was designed exclusively for PowerShell, it inherently understands how the code should be structured. Because of that ability, it can do things like providing variable PrimalSense which allows the user to auto-complete variable names, intelligently rename variables and more. In this article, let's cover how PowerShell Studio allows the user to work with variables to crank out more code and ultimately get more done.

PowerShell Studio has two features in particular that assist the user in working with variables: the debugging variable panel and scope-aware variable renaming. When working with scripts that contain lots of variables and logic, there always comes a time when debugging is necessary to figure out where the script went wrong. Using PowerShell Studio's debugging features and the variable panel is an excellent way to expose all variables that are defined and their values.

To expose the variable panel, click on the Home tab at the top and then on the Panels dropdown. In here you'll find the Variables panel.

Figure 1.

Once you've got the Variables panel open, create a script with a few variable declarations in it and start a debugging session. In the example below, I've created a script and defined two variables. I've set a breakpoint at line 3 and have run a debugging session for this script.

[Click on image for larger view.] Figure 2.

You can see that the Variables panel provides all of the variables in all scopes including the variable I've defined. This is useful in those times when debugging those large scripts.

Next is my favorite feature which is scope-aware variable renaming. This feature allows the user to intelligently rename variables instead of just performing a find/replace on the text. For example, let's say I'm reusing a variable name. I've got the variable defined in the script and one defined in a function inside the same script.

[Click on image for larger view.] Figure 3.

Perhaps I need to rename the $Variable parameter in my function. I could manually do this, but I might then have references to this variable that would break. Instead, I need to do a find/replace. I could do this by hitting Ctrl-F, filling in $Variable to find and my renamed variable to replace with but this would replace the variable name in my function as well as above in the script. Instead, I can right-click on the variable I'd like to rename and select Rename.

This presents me with a dialog box to specify the new name and the scope. You can see below that I've chosen to rename the $Variable parameter in the function and it knows this already.

[Click on image for larger view.] Figure 4.

Click OK and, by default, it will show the user all of the changes it will make. Notice that it did not find $variable defined at line 1? PowerShell Studio knows this is a different variable and should not be renamed.

[Click on image for larger view.] Figure 5.

Click Apply and the parameter variable and each reference to that variable is renamed!

PowerShell Studio is a mature product with lots of bells and whistles. In this article, we just highlighted a couple features related to variables. If you're looking for a fully-featured PowerShell editor, I suggest you check out Sapien's PowerShell Studio.

About the Author

Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.

Featured

  • Windows 10 Insider Release Includes OpenSSH Preview

  • Microsoft Teams Getting New Calling Capabilities

  • Microsoft Nudges Devs Toward Quantum Computing with New SDK

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.