Dec 212010
 

If you are writing a script that you want to (might) use again it can be really useful to use script parameters. PowerShell does some really cool things with parameters that many folks don’t know or realize you can do. This post covers some of those items.

To start us off let’s look at how to use a script parameter.

### Test-Param.ps1 ###
Param($Variable1 = "Hello", $Variable2 = "World")
"$Variable1 $Variable2"

If we save this script and run it we will get an output like this:

>>./Test-Param.ps1
>>Hello World

This simple case shows that our variables are being assigned to the default values. We can change these values by passing them to the script via the command line. These values are passed either in the order that they are sent, or statically assigned by using the variables name.

>>./Test-Param.ps1 "Goodbye"
>> Goodbye World

>> ./Test-Param.ps1 -Variable2 "Universe"
>> Hello Universe

>> ./Test-Param.ps1 "Universe" -Variable1 "Goodbye"
>> Goodbye Universe

As you can see, these are really pretty flexible. Another really cool part of params that you may have noticed by now if you are following along is that these variable names will use tab complete. So if you type “./test-param -” and then start hitting tab, you will cycle through all the names in the param block. If yo haven’t used a script in a long time, then you can quickly see what variables you might want to pass.

Now, sometimes you want to have the param feature, but you need to require the value. You can place some logic after the param block, but I like to just put code straight in there.

### Test-Param.ps1 ###
Param($Variable1 = Read-Host "Please input a value for Variable1",
      $Variable2 = Get-Content $Variable1
"$Variable1"
"$Variable2"

>> ./Test-Param.ps1
>> Please input a value for Variable1: c:ScriptTest.txt
>> c:ScriptTest.txt
>> Hello, World!

>> ./Test-Param.ps1 c:ScriptTest.txt
>> c:ScriptTest.txt
>> Hello, World!

>> ./Test-Param.ps1 c:ScriptTest.txt "Ignore my file"
>> c:ScriptTest.txt
>> Ignore my file

I think that just about covers it for this Quick Tip. Get started converting all your scripts to use a parameter block!

  3 Responses to “Quick Tip: Script Parameters in PowerShell”

  1. Nicely simplified Parameter usage 🙂

  2. Undocumented (or maybe just rarely documented, since I couldn’t find a solution) issue:

    Exit doesn’t ALWAYS exit, nor do the nuclear options you cite. Example:

    I have a script that acts as a menu for all the rest of my scripts. All my other scripts are written to take in a single parameter. If that parameter is present when the script goes to exit, it goes back to the main menu. This parameter is only sent from the Menu script, so all the sub-scripts run and exit normally by themselves.

    Now comes the fun part: Launch script1 (Menu) and from it, launch a sub-script. The second script runs, and since the parameter is set by the first script, when it exits, it re-launches the first (Menu) script, again. Now, I have an option written into the first (Menu) script that, if used, should exit that menu script under normal conditions. However, the second script will pick back up where it left off and continue as if nothing had happened. It is, I’m assuming, as you mentioned, a scoping issue. But, even your nuclear options don’t kill all scopes.

    The only answer I found was to kill the powershell process altogether: Stop-Process -Name “powershell”

    This is a bit of cluge, especially if I want to do other things in the shell afterwards. But, in this instance, my goal was achieved.

  3. Ryan,

    Thank you for a straight forward explanation on using parameters within PowerShell. Your information is helping me brush up on POSH for a future work project. Here is something I have discovered while running your code under PowerShell v5 requires the use of sub-expressions in the parameter block when dealing with full commands.

    Example:

    Param($Variable1 = $(Read-Host “Please input value for variable1”), $Variable2 = $(Get-Content $Variable1))

    v/r
    Jason

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.