PowerShell Tip: Add Parameters to Registry for Easy Access

I tend to work on large PowerShell scripts that accept parameters. In the past I’ve always defined these parameters in the script while I test, and then I have to go back and remove them when I blog about the script here. I decided I’d figure out an easier way to do this!

I’m already using my ISE addon to run scripts in a new PowerShell session, so I decided to edit that to add parameter support!

First off, I only care to do this for saved scripts, so I only have to edit the “else” statement in my ISE addon:

I ended up deciding to store these variables in the registry. I post a lot of my projects to GitHub and didn’t want a file full of variables in my Git repo. So I went to my registry and created a new Key under HKCU:\Software called EphingParams. I then need a way to tell it what script I’m using. The current project I’m working on has a lot of scripts in the same directory all using a similar set of parameters. So I created a key with the name of the folder “ConfigMgr_Console_Extensions”

image

The idea is, my launch script will check to see what the file path is. If the file path includes “\ConfigMgr_Console_Extensions\” (meaning it is in that folder) or the file path includes “\ConfigMgr_Console_Extensions.” (meaning it is the file name) then it will grab the parameters. Now I just need to use the registry editor to make a few test parameters:

image

I’ve finished with the setup! Now I need to add the logic to my ISE addon:

This script block will get all registry keys in HKCU:\Software\EphingParams, and then check the filepath to see if it includes a child registry key. If it does, it grabs all the properties (excluding the 4 default properties) and adds them to the launch code as  -PropertyName “PropertyValue”. The end result is your script being executed with the parameters in the registry!

Here’s the full script:

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *