Passing Parameters to a PowerShell Script

Written by Barry Dysert (last updated January 9, 2017)

4

You can pass parameters to a PowerShell script two different ways. (If you don't know about PowerShell, please see the tip, "Introduction to PowerShell".) Parameters can be passed by position or by name. Both are equally valid, so let's look at how each is done.

I have a PowerShell script named LookForFiles.ps1. It looks for files in a given directory and displays those files that are greater than 20K in size. Right now my script looks like this:

$files = Get-ChildItem C:\Temp
foreach ($file in $files)
{
    if ($file.length -gt 20000)
    {
        Write-Output $file
    }
}

As you can see, my directory name (C:\Temp) and the size to check (20000) are both hard-coded. I want, instead, to pass in the directory name and size so that my script will be more flexible.

First, I'll change things so that I pass these parameters by position, i.e., the first parameter will be the directory, and the second parameter will be the size. My new script looks like this:

$files = Get-ChildItem args[0]
foreach ($file in $files)
{
    if ($file.length -gt args[1])
    {
        Write-Output $file
    }
}

Now I invoke my script and observe the output: (See Figure 1.)

Figure 1. Passing positional parameters.

Notice that when I invoke the script I pass two parameters, viz., "C:\Temp" and "20000". These are filled into "$args[0]" and "$args[1]" respectively so that the script executes exactly as before. Now let's modify the script so that it expects named parameters:

param([string] $dir = "C:\Temp", 
      [int32] $size = 20000)
$files = Get-ChildItem $dir
foreach ($file in $files)
{
    if ($file.length -gt $size)
    {
        Write-Output $file
    }
} 

Notice that I removed the references to the "$args" variables. Note too that the first line (extended to the second line) is a "param" statement. This tells PowerShell to expect named parameters. This "param" statement must be the first line in your script. It defines two variables and initializes them to default values. It defines the string variable $dir and defaults it to "C:\Temp" and it defines the int32 (numeric) variable $size and defaults it to 20000. These values are initialized with default values so that if no parameters are passed when the script is invoked, it will still run and use those values.

In place of where I used to have $args[0] I now have the $dir variable and in place of where I used to have $args[1] I now have the $size variable. Let's invoke the script, specifying these named parameters: (See Figure 2.)

Figure 2. Passing named parameters.

Now when the script is invoked I specify "-dir" followed by the directory I'm interested in, and I specify "-size" to indicate the size I'm interested in. Since these are named parameters I can reverse their order so that "-size" is specified first and "-dir" is specified second. Or I can omit them entirely and the values inside the script will be used.

 This tip (2902) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

Creating Your Own File Folders

In the general course of using your system, you will likely create many new files. It's a good idea to organize your ...

Discover More

Understanding Desktop Widgets

Widgets are small, lightweight applications that continually run on your desktop to provide information. You can add, ...

Discover More

Using the ChkDsk Utility

The ChkDsk utility is a nice feature of Windows that lets you keep tabs on the health of your disk drives. This tip tells ...

Discover More
More WindowsTips

Using PowerShell to Find Your Wireless Signal Strength

Wi-Fi signal strength is important for faster, more consistent data transfers. You can use PowerShell to gain a more ...

Discover More

Change your Computer's Name with PowerShell

Your computer has a name that is separate from your account name. You can use PowerShell to change the name of your computer.

Discover More

Counting the Number of Files or Subfolders Using PowerShell

Do you need to determine the number of files or subfolders there are in a folder? You can use PowerShell to quickly count ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

2019-08-08 17:15:44

Ed

Excellent tips - thanks!


2018-10-25 11:19:22

Jin

Thanks a lot for your tip, Barry. There are typos to point out. In your sample script, the $ was missed from $args[x]. I copied your sample code to test. It did not work. It took me some time to figure out it was because the $ was missed ;-).


2018-02-22 22:18:12

Panny

Good examples, and very nice and simple explanation


2017-01-09 13:17:26

James

These PowerShell tips are great. Keep them coming!


Newest Tips