PowerShell Param Explained with Examples

Photo of author

By Victor Ashiedu

Published

PowerShell script developers use Powershell Param block to add parameters to advanced functions. Learn how to add parameters to your PowerShell scripts and advanced functions.

Syntax of a PowerShell Param Block

The syntax of PowerShell Param is…

[CmdletBinding(DefaultParameterSetName=<value>,
SupportsShouldProcess=$true | $false ) ]
Param(
[Parameter(Mandatory=$true | $false, 
ValueFromPipeline=$true | $false, 
Position=<value>, 
ParameterSetName = 'ParameterSetName')] 
[Alias("<value>")]
[ValidateNotNullOrEmpty()]
[data type]$parametername
) 

To add a parameter to a function, begin with the word Param, followed by the parameter block, (). Here is a simple form of a param block…

[CmdletBinding()]
Param(
     [Parameter()]
     $ParameterName
 )
The CmdletBinding attribute is optional and it is the only attribute defined outside the param block. More on this later in the guide

Within the Param block – param () – you will define the Parameter attributes and their arguments. Furthermore, a parameter attribute is enclosed in a [()] block.

In the syntax code above, I bolded all the parameter arguments. There are a lot more parameter arguments – the ones in the syntax are for illustration purposes only and are not in any way exhaustive.

Within the parameter block, you will define parameter arguments, separated by commas. An example of a PowerShell Param attribute is the parameter attribute.

Additionally, an example of a param argument is the mandatory argument. Another example of a param argument is ParameterSetName.

Finally, you will define the name of the parameter. Moreover, you will also specify the parameter data type.

In the next section, I will discuss the common types of PowerShell parameters. Then, section three covers common parameter attributes and their arguments.

Finally, in the last section of this guide, I will give a real-life SysAdmin example of a PowerShell Param. The section will also give examples of how to use the param block in an Advanced Function.

Types of PowerShell Parameters

The most common types of parameters you can set are string or switch parameters. Additionally, you can also define int, bool, DateTime, or char parameter types.

In this section, I will explain some of these types of parameters. I will also give some examples of how to define them in a param block.

String Parameter

When you create PowerShell parameters, it is likely that you will create a string parameter. A string parameter accepts any string value.

In Windows PowerShell, a string can be numbers, words or characters. Specifically, anything you declare in a double or single quote is a string.

In PowerShell, the object type of a string is System.String.

To declare a PowerShell parameter as a string parameter, before the name of the parameter, enclose the word string in [] brackets. Here is an example…

[string]$ReportPath

int Parameter

An int data type is a 32-bit signed integer. In English, an int is a number, for example, 1, 2, or 3.

Unlike a string parameter data type that can take any type of data, an int parameter is very prescriptive.

If you define an int parameter, the parameter can only accept numbers. Furthermore, if you enter words as the value for the int parameter, PowerShell will throw an error message.

Before I show you how to define a PowerShell Param int parameter, here is a general example of how to define an int data type…

[int]"123"

If I run the code in PowerShell, it will return 123.

Powershell int param Parameter

On the contrary, if I replace, 123 with “this is an int”, PowerShell will throw an error message.

[int]"this is an int"
appending string to int throws an error message

Now that you have had an int data type crash course, to create a PowerShell Param int parameter called number, here is the code…

[int]$Number

This will force the users of your script or function to provide the right data type.

DateTime Parameter

Like an int parameter, a DateTime parameter is also very prescriptive of the type of input it accepts. As the name implies, the DateTime parameter will only accept inputs of Date and Time format.

If you want to define a PowerShell parameter that will only accept dates, use the sample code below…

[DateTime]$EnterDate

In this example, EnterDate is the name of the parameter. Additionally, DateTime defines the data type.

Switch Parameter

In the first three sub-sections of this section, I have discussed PowerShell parameter types that require input from a user.

However, there is a PowerShell parameter type that does not require input from a user. Unlike the other types of parameters, a switch parameter does not require user input.

The way you define a switch parameter is similar to the way you define other types of parameters. However, in place of switch, int, or datetime, use the word switch.

For example to define a PowerShell param Switch parameter, ExportCSV, use the code below…

[Switch]$ExportCSV

Finally, for this section, you may be wondering about the application of switch parameters. Think of a PowerShell script or function that can display results on the Powershell console.

You may also want to give the user the option to either export the result to a text file or a CSV file.

One way to code the function is to create 3 switch parameters. A user can then select one of the parameters, depending on how they want the function to deliver the result.

This type of parameter does not require the user to enter an input. By simply selecting the parameter, the function will display the output accordingly.

You can also define other PowerShell parameters with specific data types. To learn about PowerShell data types, visit this link – How to Define PowerShell Data Types.

Parameter Attributes and Arguments

In the PowerShell Param Syntax of this guide, I hinted that a PowerShell parameter has attributes. Moreover, I also pointed out that each parameter attribute also has arguments.

In this section, I will be discussing parameter attributes and their arguments.

To explain parameter attributes and their arguments, I will be referring to the syntax of the PowerShell param introduced in the first section of this guide.

[CmdletBinding(DefaultParameterSetName=<value>,
SupportsShouldProcess=$true | $false ) ]
Param(
[Parameter(Mandatory=$true | $false, 
ValueFromPipeline=$true | $false, 
Position=<value>, 
ParameterSetName = 'ParameterSetName')] 
[Alias("<value>")]
[ValidateNotNullOrEmpty()]
[data type]$parametername
) 

CmdletBinding Attribute

The CmdletBinding attribute is usually specified before the param block. When you define the CmdletBinding attribute, PowerShell will recognize the script as an advanced function.

In the syntax section of this guide, I used this code as a simplified version of a param block.

[CmdletBinding()]
Param(
     [Parameter()]
     $ParameterName
 )

In this section, I will focus on the CmdletBinding Attribute portion of the code. Here is how to define the Attribute

[CmdletBinding()]

In the next 3 sub-sections, I will discuss some of the common arguments, you can define in the CmdletBinding attribute.

For the avoidance of doubt, you must define all Arguments within the () block of the Attribute. Also, you MUST separate all attributes by a comma (,), except the last Arguments within the Attribute.

DefaultParameterSetName Argument

When you develop a PowerShell function, it can contain multiple parameters that a user can use in the same command. Additionally, the same PowerShell function can contain another set of parameters that a user can use in another command.

However, the user cannot mix the parameters in the first set with the parameters in the second set.

Furthermore, a function developer defines all parameters that a user can use in the same command with the ParameterSetName argument. The ParameterSetName argument is defined within the Parameter Attribute (more on this in the next section).

The implication of having different sets of “grouped” parameters is that you will have different ParameterSetNames.

With different ParameterSetNames defied in your param block, you may decide to define one of the ParameterSetNames as the default. This is where you use the DefaultParameterSetName Argument.

The DefaultParameterSetName Argument is defined within the CmdletBinding attribute block. Here is an example…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1")]

Finally, to retain our PowerShell param block format, I will reintroduce the param block below…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1")]
Param(
     [Parameter()]
     $ParameterName
 )

SupportsShouldProcess Argument

This is another argument that you can define in the CmdletBinding Attribute. Like the DefaultParameterSetName Argument, the SupportsShouldProcess argument is optional.

The SupportsShouldProcess Argument adds the Confirm and WhatIf common parameters to the function.

To add this argument, add a comma (,) to the end of the DefaultParameterSetName argument. Then, add the line – SupportsShouldProcess=$true or $false.

With this new argument included, the CmdletBinding Attribute will now look like this…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true)]
Param(
     [Parameter()]
     $ParameterName
 )

HelpURI Argument

If you are familiar with the PowerShell Get-Help Cmdlet, you may have come across its Online parameter. When you run Get-Help with the Online parameter, PowerShell opens the online help page of the Cmdlet.

For example, to open the online help page of the Out-File Cmdlet, run the command below…

Get-Help Out-File -Online 

The page opened by this command is defined in a function by the HelpURI Argument. So, if you want users to access the online help page of your function, define the HelpURI Argument.

Here is an example…

HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/"

I will now include this last Argument in the CmdletBinding Attribute. Here is the updated code…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter()]
     $ParameterName
 )

Parameter Attribute

In the “PowerShell Param CmdletBinding Attribute” sub-section, I discussed 3 arguments that you can include in the CmdletBinding Attribute block.

As you have seen, the CmdletBinding Attribute block is the only attribute defined outside the param block.

In this sub-section, you will learn about the Parameter Attribute. Additionally, you will also learn about the common Arguments you can add to this Attribute.

As you may have deduced from the examples already used in this guide, the Parameter Attribute defines arguments that control the behavior of a PowerShell param parameter.

You can define the Parameter Attribute by enclosing the word Parameter, followed by () brackets in a [] block. Here is an example…

Param(
[Parameter()]
$ParameterName
)

Moving on, you MUST define all the arguments of the Parameter Attribute within the () block.

In the following sub-sections of the “PowerShell Param Parameter Attribute” section, I will introduce and explain the common arguments you can define within the Parameter Attribute.

ParameterSetName Argument

When I explained DefaultParameterSetName earlier in this guide, I introduced ParameterSetName Argument. You can use this argument to “batch” or “group” parameters that a user can run in the same command.

For example, to define a ParameterSetName called ParameterSetName1, enter the code below in the Parameter Attribute () block.

ParameterSetName='ParameterSetName1'

When I introduce this updated code, the param block of my sample code will now look this this..

Param(
     [Parameter(ParameterSetName='ParameterSetName1')]
     $ParameterName
 )

If I bring back the CmdletBinding attribute, my sample PowerShell param code will now look like this…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1')]
     $ParameterName
 )

Mandatory Argument

You can use the Mandatory argument to define whether a parameter is required or optional. The Mandatory argument accepts a $true or $false value – $true means require, $false, not required.

Furthermore, the Mandatory argument is optional. However, if this argument is not defined, the parameter will be treated as optional.

To define the Mandatory Argument, add the code below to the Parameter Attribute block…

Mandatory=$true

Finally, if I include the Mandatory Argument code to my PowerShell param code, I will now have…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true)]
     $ParameterName
 )

Position Argument

The Position Argument of the Parameter Attribute defines the position of the parameter. The value is an integer, starting from 0 (zero).

The benefit of the Position argument is to allow users to run the function using the position of the parameters. Specifically, when you define the Position Argument, a user can enter values for each parameter in the command line without specifying the parameter.

PowerShell will then assign each value in the command line according to the parameter position.

For example, the Get-ChildItem Cmdlet has a parameter called Path (with position 0). The Cmdlet also has another parameter, Filter (with position 1).

By default, if I run the Get-ChildItem command, I have to include the parameter names before adding their values. See the command below…

Get-ChildItem -Path D:\reports -Filter *.txt

In this command, I called the parameters and then specified their values.

Powershell param position parameter attribute - ex 1

However, since I know the positions of these 2 parameters, I can add values in their positions, without expressly calling the parameters. I will then modify my command as shown below…

Get-ChildItem D:\reports -Filter *.txt

Without a doubt, this saves me time! However, if you are looking at a long PowerShell code, this may cause some confusion.

The command gives the same result in PowerShell…

PowerShell will automatically assign the values in the positions to the parameters based on their Position Argument.

Moving on, here is how you define the Position Argument…

Position=0
In this example, my parameter position is 0 (zero). When I define my next parameter, it will be in position 1, and so on. You cannot have two parameters in the same position. Later in the guide, you will learn how to add one parameter in the same position but different ParameterSetNames.

I will now add this argument to my sample code…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true,
      Position=0)]
     $ParameterName
 )

ValueFromPipeline Argument

Another commonly used parameter augment is the ValueFromPipeline Argument. As the name implies, this argument determines whether a parameter accepts values from a pipeline.

Here is how you define this argument…

ValueFromPipeline=$true or $false

If I add this new argument to my code, it will look like this…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true,
      Position=0,
     ValueFromPipeline=$true)]
     $ParameterName
 )

There is another argument called ValueFromPipelineByPropertyName. To read about this argument, visit about_Functions_Advanced_Parameters.

HelpMessage Argument

You can use this argument to specify the message to show users how to use a parameter. To define a HelpMessage Argument for a parameter, use a code similar to the one below…

HelpMessage="Specifiy your message here"

And here is now my updated code…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true,
      Position=0,
     ValueFromPipeline=$true,
     HelpMessage="Specifiy your message here")]
     $ParameterName
 )

So far in this section, I have discussed 2 PowerShell param attributes – CmdletBinding and Parameter. Additionally, I also discussed the common arguments you can define for these attributes.

In the remaining 3 sub-sections of this section, I will teach you 3 more commonly used PowerShell param attributes. However, these 3 attributes do not require you to define arguments.

Alias Attribute

The alias attribute is used to specify the aliases of a parameter. As you may have guessed, the alias of a PowerShell parameter can be used instead of the parameter.

Before I show you how to define the alias attribute for a parameter, let me first show you how to find aliases of all Cmdlets, and Functions on your computer.

To list all the aliases for Cmdlets and Functions, run the command below

Get-Alias

This will list all aliases…

Alias Attribute

Moving on to Alias Attribute, to define one for a PowerShell param parameter, use a code similar to the one below…

[Alias("FP")]
In this example, FP is the alias for the parameter.

If I add this to my code, the updated version will look like this…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true,
      Position=0,
     ValueFromPipeline=$true,
     HelpMessage="Specifiy your message here",
     [Alias("FP")])]
     $ParameterName
 )

SupportsWildcards Attribute

This parameter param attribute determines whether a parameter accepts wildcards. Here is how you define this attribute…

[SupportsWildcards()]

ValidateNotNullOrEmpty Validation Attribute

Like most attributes already covered in this guide, the name of this attribute also gives it away. It is a validation attribute.

When you define this attribute, the parameter will NOT accept a null ($null) value. Additionally, it will not accept an empty (“”) value.

If a user enters $null, or “”, the function will throw an error. To define this attribute, enter the code below…

[ValidateNotNullOrEmpty()]

Finally, I will update my code with the last 2 attributes…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true,
      Position=0,
     ValueFromPipeline=$true,
     HelpMessage="Specifiy your message here",
     [Alias("FP")])]
     [SupportsWildcards()]
     [ValidateNotNullOrEmpty()]
     $ParameterName
 )

PowerShell Parameter Name

After you have defined all the attributes and arguments of a parameter, you need to add the parameter name. A PowerShell param parameter is defined like a variable – with a dollar sign ($), followed by the name of the parameter.

You can also optionally enter a data type for the parameter. In section 2 of this guide, I discussed different types of PowerShell parameters.

Here is how you define a parameter that can accept string data type.

[string]$ParameterName

Here is my updated code example…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
     [Parameter(ParameterSetName='ParameterSetName1',
      Mandatory=$true,
      Position=0,
     ValueFromPipeline=$true,
     HelpMessage="Specifiy your message here",
     [Alias("FP")])]
     [SupportsWildcards()]
     [ValidateNotNullOrEmpty()]
     [string]$ParameterName
 )

How to Define Parameter Sets

If you are new to coding PowerShell functions, chances are that you will fall into the temptation of defining one parameter multiple times.

One reason you may want to have a parameter multiple times is when a parameter is used in multiple ParameterSetNames (multiple parameter sets).

To drive this home, let’s say you are developing a PowerShell function that has a Path parameter. However, you have 2 ParameterSetNames that require the Path parameter.

Most people new to PowerShell scripting will likely define the parameters shown below…

[CmdletBinding(DefaultParameterSetName="ParameterSetName1")]
Param(
      [Parameter(ParameterSetName='ParameterSetName1',
       Mandatory=$true,
       Position=0)]
       [string]$Path,
       [Parameter(ParameterSetName='ParameterSetName2',
       Mandatory=$true,
       Position=0)]
       [string]$Path
  )

If you define the parameters, as shown above, you will receive an error message. To test this theory, I will copy the code to PowerShell ISE.

How to Define Parameter Sets - duplicate parameter error

So, how do you fix this? The solution is to define the 2 parameter attributes and their arguments – including their ParameterSetNames.

Then, at the end of both parameter attributes, define a single parameter name that is common to both parameter attributes. In the previous example, I will remove the first parameter name and leave the last one.

[CmdletBinding(DefaultParameterSetName="ParameterSetName1")]
Param(
      [Parameter(ParameterSetName='ParameterSetName1',
       Mandatory=$true,
       Position=0)]
      [Parameter(ParameterSetName='ParameterSetName2',
       Mandatory=$true,
       Position=0)]
       [string]$Path
  )

Now if I copy this code to PowerShell ISE, it will be okay.

In this example, note that the parameter arguments for both parameter attributes can be completely different. Additionally, you can define as many parameter attributes as you want.

PowerShell Param Examples

In this last section of the guide, I will give a real-life SysAdmin PowerShell param example.

While I was writing this guide I started developing a PowerShell script/function to get folder size and file count. Here is the param part of the function…

[CmdletBinding(DefaultParameterSetName="DisplayResult",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
[Parameter(
       ParameterSetName='DisplayResult',
       Mandatory=$true,
       Position=0,
       HelpMessage="Specifiy the folder path to calculate size and file count for its sub-folders",
       ValueFromPipeline=$true,
       ValueFromPipelineByPropertyName=$true)]
 [Parameter(
       ParameterSetName='ExportTxt',
       Mandatory=$true,
       Position=0,
       HelpMessage="Specifiy the folder path to calculate size and file count for its sub-folders",
       ValueFromPipeline=$true,
       ValueFromPipelineByPropertyName=$true)]
   [Parameter(
        ParameterSetName='ExportCSV',
        Mandatory=$true,
        Position=0,
        HelpMessage="Specifiy the folder path to calculate size and file count for its sub-folders",
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [Alias("FP")] #attribute must appear once 
        [ValidateNotNullOrEmpty()] 
        [string[]]$FolderPath, #can specify multiple paths separated by commas
    [Parameter(
        ParameterSetName='DisplayResult',
        Mandatory=$true,
        Position=1,
        HelpMessage="USe this parameter to display result on the console",
        ValueFromPipeline=$false,
        ValueFromPipelineByPropertyName=$false)]
        [Alias("DR")]
        [Switch]$DisplayResult,
    [Parameter(
        ParameterSetName='ExportTxt',
        Mandatory=$true,
        Position=1,
        HelpMessage="USe this parameter to send result to a text file",
        ValueFromPipeline=$false,
        ValueFromPipelineByPropertyName=$false)]
        [Alias("ExTxt")]
        [ValidateNotNullOrEmpty()]
        [Switch]$ExportTxt,
   [Parameter(
        ParameterSetName='ExportCSV',
         Mandatory=$true,
         Position=1,
         HelpMessage="USe this parameter to send result to a CSV file",
         ValueFromPipeline=$false,
         ValueFromPipelineByPropertyName=$false)]
         [Alias("ExCSV")]
         [ValidateNotNullOrEmpty()]
         [Switch]$ExportCSV,
[Parameter(
         ParameterSetName='ExportTxt',
         Mandatory=$true,
         Position=2,
         ValueFromPipeline=$false,
         ValueFromPipelineByPropertyName=$true)]
[Parameter(
         ParameterSetName='ExportCSV',
         Mandatory=$true,
         Position=2,
         ValueFromPipeline=$false,
         ValueFromPipelineByPropertyName=$true)]
         [ValidateNotNullOrEmpty()] #attribute must appear once
         [string]$ReportPath

 )

In the next few subsections of this section, I will explain the different elements of the param. Finally, the last section introduces a function block and adds the param block to the function.

To make it easy for you to follow the lines on the code referenced in the next sub-sections, download PowerShellParamExample.zip. Then, unzip the file and open the .ps1 file in PowerShell ISE.

1. ParameterSetName PowerShell Param Example

My PowerShell param example code has three ParameterSetNames – DisplayResult, ExportTxt, and ExportCSV. I used these names to make it easy to identify what they do.

In the introduction of this section, I mentioned that the function I am building with this param code will display folder size and file count for each sub-folder in a specified folder.

Therefore, the three ParameterSetNames offer a user the option to display results on the PowerShell console. Alternatively, a user has the option to export the result to a text file or a CSV file.

Furthermore, each ParameterSetName defines all parameters that a user can call in a single command. Based on this idea, the function I am building will have three command options or syntaxes.

I will expand on this idea in the subsequent subsections and examples.

2. CmdletBinding Attribute and DefaultParameterSetName Argument Examples

In the previous sections of this guide, you came across the CmdletBinding Attribute. Moreover, you also came across one of its arguments – DefaultParameterSetName.

The DefaultParameterSetName argument is used to define the default parameter set that a function calls. In this example, the default parameter set name is DisplayResult.

The DefaultParameterSetName is declared in line 1 of the code.

PowerShell will attempt to use the parameter set declared by the DefaultParameterSetName if it cannot determine which parameter set to use. However, if you set mandatory parameters, you can avoid this problem.

In any case, there is no harm in declaring a default parameter set name.

3. FolderPath String Parameter Example

This is where the fun begins! The FolderPath parameter is used to input the folder path the function will get the size of its sub-folders – and count the number of files in each sub-folder.

This is a string parameter. Meaning that it accepts any string as input.

One exciting feature of the FolderPath parameter is that it is common to the 3 parameter set names. Earlier in this article, I explained the challenge that new PowerShell script developers may face with this situation.

To declare the FolderPath parameter in the three parameter set names, I created three parameter attributes.

The first parameter attribute is between lines 5 and 11. Additionally, this first parameter attribute has multiple arguments.

However, I would like you to pay particular attention to the ParameterSetName argument – DisplayResult.

Secondly, between lines 12 and 18, I declared another parameter attribute with a ParameterSetName argument called ExportTxt.

Finally, between lines 19 and 25, I declared a third parameter attribute with a ParameterSetName argument called ExportCSV.

lines 19 and 25, I declared a third parameter attribute with a ParameterSetName argument called ExportCSV.

Moreover, if you notice, unlike declaring 3 parameter attributes, I declared a single Alias and ValidateNotNullOrEmpty attributes. These attributes can be found in lines 26 and 27.

Finally, just below all the attributes and their arguments, I included the name of the parameter – FolderPath. The parameter name is in line 28.

In the last subsection of this section, you will see how the function displays this parameter in 3 command syntaxes.

4. DisplayResult, ExportTxt, and ExportCSV Switch Parameter Example

From line 29 of the param code, I declared another set of parameter attributes and their arguments.

Firstly, between lines 29 and 37, I declare the parameter attributes and arguments for the DisplayResult parameter.

Then, between lines 38 and 47, I declare another parameter called ExportTxt.

Finally, between lines 48 and 57, I declared the ExportCSV parameter.

lines 48 and 57, I declared the ExportCSV parameter

The 3 parameters I have described in this section are all Switch parameters. As you already know, this means that the parameters do not require any input from the user.

When a user selects any of the parameters, the function changes the way it displays results.

How to Use the Param Keyword in a PowerShell Function

In this sub-section, I want to introduce the PowerShell function block into my example code.

The syntax of a PowerShell function block is…

Function  <name>  {
  
    Param(

    )

   Begin {

 }

    Process {

    }

  End {

   }


}

For the final example in this guide, I will replace the PowerShell param block within the syntax of the function with my param code from the last sub-section. Additionally, I will replace the function <name> with Get-FolderSizeFileCount, the name of the function.

The final function will now look like this…

Function  Get-FolderSizeFileCount {

[CmdletBinding(DefaultParameterSetName="DisplayResult",
SupportsShouldProcess=$true,
HelpURI="https://www.itechguides.com/category/technology-explained/powershell-cmd-explained/")]
Param(
[Parameter(
       ParameterSetName='DisplayResult',
       Mandatory=$true,
       Position=0,
       HelpMessage="Specifiy the folder path to calculate size and file count for its sub-folders",
       ValueFromPipeline=$true,
       ValueFromPipelineByPropertyName=$true)]
 [Parameter(
       ParameterSetName='ExportTxt',
       Mandatory=$true,
       Position=0,
       HelpMessage="Specifiy the folder path to calculate size and file count for its sub-folders",
       ValueFromPipeline=$true,
       ValueFromPipelineByPropertyName=$true)]
   [Parameter(
        ParameterSetName='ExportCSV',
        Mandatory=$true,
        Position=0,
        HelpMessage="Specifiy the folder path to calculate size and file count for its sub-folders",
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [Alias("FP")] #attribute must appear once 
        [ValidateNotNullOrEmpty()] 
        [string[]]$FolderPath, #can specify multiple paths separated by commas
    [Parameter(
        ParameterSetName='DisplayResult',
        Mandatory=$true,
        Position=1,
        HelpMessage="USe this parameter to display result on the console",
        ValueFromPipeline=$false,
        ValueFromPipelineByPropertyName=$false)]
        [Alias("DR")]
        [Switch]$DisplayResult,
    [Parameter(
        ParameterSetName='ExportTxt',
        Mandatory=$true,
        Position=1,
        HelpMessage="USe this parameter to send result to a text file",
        ValueFromPipeline=$false,
        ValueFromPipelineByPropertyName=$false)]
        [Alias("ExTxt")]
        [ValidateNotNullOrEmpty()]
        [Switch]$ExportTxt,
   [Parameter(
        ParameterSetName='ExportCSV',
         Mandatory=$true,
         Position=1,
         HelpMessage="USe this parameter to send result to a CSV file",
         ValueFromPipeline=$false,
         ValueFromPipelineByPropertyName=$false)]
         [Alias("ExCSV")]
         [ValidateNotNullOrEmpty()]
         [Switch]$ExportCSV,
[Parameter(
         ParameterSetName='ExportTxt',
         Mandatory=$true,
         Position=2,
         ValueFromPipeline=$false,
         ValueFromPipelineByPropertyName=$true)]
[Parameter(
         ParameterSetName='ExportCSV',
         Mandatory=$true,
         Position=2,
         ValueFromPipeline=$false,
         ValueFromPipelineByPropertyName=$true)]
         [ValidateNotNullOrEmpty()] #attribute must appear once
         [string]$ReportPath

 )

Begin {

 }

    Process {

    }

  End {

   }

}
For this example, the Begin {}, Process {}, and End {} blocks are empty.

Testing the Parameters in My Function

In this final section, I will test the parameters in the function I created in the last section.

If you like to test it yourself, you can download the code by clicking this link – Get-FolderSizeFileCount.zip. Then, unzip the zip file.

Once you unzip the file, open PowerShell and run the command below:

Before running the script, you may need to set the execution policy by running “Set-ExecutionPolicy -ExecutionPolicy Undefined -Force -Confirm:$false”
Import-Module <path>\Get-FolderSizeFileCount\Get-FolderSizeFileCount\Get-FolderSizeFileCount.psm1
Change <path> to the full path to the unzipped file.

When you run the command, you will receive a warning message. At the message prompt, enter R and press the enter key on your keyboard.

Once the module is imported, on the same PowerShell console, run the command below:

get-help Get-FolderSizeFileCount

Here is the result of the command…

What I want to show you is the SYNTAX section of the help page of my Function. From the screenshot above, you can run the Get-FolderSizeFileCount command in 3 different commands:

Get-FolderSizeFileCount [-FolderPath] <string[]> [-DisplayResult] [-WhatIf] [-Confirm]  [<CommonParameters>]

Get-FolderSizeFileCount [-FolderPath] <string[]> [-ExportCSV] [-ReportPath] <string> [-WhatIf] [-Confirm]  [<CommonParameters>]

Get-FolderSizeFileCount [-FolderPath] <string[]> [-ExportTxt] [-ReportPath] <string> [-WhatIf] [-Confirm]  [<CommonParameters>]

The first thing to note is that the three syntaxes have the FolderPath string parameter. If you read through this guide, you will know why.

Additionally, you will notice that the first syntax has the DisplayResult switch parameter as well. Finally, in the second and third syntaxes, you have the ExportCSV, and ExportTxt switch parameters respectively.

In addition to the FolderPath, ExportCSV parameters, and ExportTxt parameters in the second and third syntaxes respectively, they also have the ReportPath string parameter. I did not mention this parameter because the declaration code is similar to the FolderPath code, with some minor differences. The ReportPath is used to specify the path the function will save the text and CSV file reports.

Conclusion

That is it – a comprehensive guide about PowerShell param! I hope you found it helpful.

Let me know by responding to the “Was this page helpful?” question below. You could also join the conversation with our community by leaving a comment using the “Leave a Reply” form situated at the bottom of this page.

Before you jump out, why not browse our Windows PowerShell how-to guide page for more hands-on articles?

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

4 thoughts on “PowerShell Param Explained with Examples”

  1. Exactly what I was looking for. So well-detailed!
    Of course there are some sections I can’t wrap my head around but I think with a little more time I will.
    Thank you Mr. Ashiedu for your work. I swear it’s been really helpful and also my top bookmark

    Reply
  2. I’m surprised there aren’t comments here, yet. This was a fabulous overview, Victor. Thank you for taking the time to publish this. I’ll be working through this to understand another script I’ll need to customize for use with Azure blob archive storage. Your article will be very helpful in assisting me with that. Thanks, again!

    Reply

Leave a comment

Send this to a friend