Working with Variables in Sapien's PowerShell Studio
This tool makes it easy to create PowerShell GUIs quickly.
- By Adam Bertram
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.
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.
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.
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 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 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.
Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.