PowerShell Parameters Explanations & Examples

Spread The Knowledge

Welcome Back, Techies! If you are looking for some useful guide to PowerShell Parameters, then you are in the right place. Today we are going to explain PowerShell Parameters and its various types. Every cmdlet is associated with one or more parameter/s. Parameters offer a mechanism to allow a cmdlet to perform certain actions, hence retrieving the desired information.

Every parameter of a cmdlet comprises a switch and a value as shown below.

Get-Help -Name Get-Command

Types of PowerShell Parameters:

Windows PowerShell has three types of parameters:

  • Optional parameter
  • Mandatory parameter
  • Positional parameter

Optional Parameter

Outer square brackets around the parameter (switch and value) indicate that this parameter is optional.

Cmdlets with optional parameters can be executed with or without the parameter.

Get-Command [[-Name] <String>]
  • Execute the following two cmdlets and observe the output.
Get-Command
Get-Command -Name Get-Help

Highlights: The cmdlets with optional parameters having its switch also enclosed within square brackets can be executed without specifying it. The cmdlets with optional parameters having its switch not enclosed in square brackets make it mandatory. Demosteps:

Step 1: Since –Name(Switch) in the parameter is also enclosed within square brackets, the cmdlet can be executed even without the switch.

Get-Command Demo

Step 2: Get-Help -Name Get-Command can be executed as Get-Help Get-Command as -Name is enclosed with square brackets.

Get-Help Get-Command

Step 3: Since –Verb(Switch) in the parameter is not enclosed within square brackets, the switch becomes mandatory.

Get-Command [-Verb <string[]>]

Step 4: Get-Command Get throws an error because -Verb is not specified.

Get-Command Errors

Mandatory parameter

Parameters with no outer square brackets around the parameter (switch and value) indicate that it is a mandatory parameter.

Cmdlets with mandatory parameters cannot be executed without specifying a value for the parameter.

Get-EventLog [-LogName] <string>
power

Example:

Get-EventLog -LogName Application

Highlights: Mandatory parameters can also have optional switches. The cmdlets with mandatory parameters having its switch enclosed within square brackets can be executed even without the switch. Demosteps:

Step 1: Since the –LogName(Switch) in the parameter is also enclosed within square brackets, the cmdlet can be executed even without the switch.

Mandatory parameter without switch

Step 2: The -LogName parameter need not be specified as shown in the below example.

Get-EventLog Application

Positional Parameter

A positional parameter requires that you type the arguments in relative order.

The system then maps the first unnamed argument to the first positional parameter, the second unnamed argument to the second unnamed parameter, and so on.

PowerShell Positional Parameter
  • -LiteralPath specifies the source and –Destination specifies where the value has to be copied.

Example:

Copy-Item –LiteralPath D:\File1.txt –Destination D:\File2.txt

Contents of File1 is copied to File2 in the above example.

Highlights: The order in which the parameter names are specified can be interchanged. If the parameter names are not specified, the cmdlet will behave in its default manner. Demosteps:

Step 1: In the below example, the contents of File2 is copied to File1.

Copy-Item

Step 2: If the parameter names are not specified, the Copy-Item cmdlet will behave in its default manner. The first parameter is always the Source and the second parameter is the Destination.

Source and Destination

Step 3: In the below example, contents of File2 is copied to File1.

Positional Parameter Example

Source : Microsoft

Continue Learning:

  1. Transfer Files Between Windows & Linux Machine Using PowerShell
  2. An Introduction to VMware PowerCLI
  3. Disable Clipboard Redirection Multiple Servers PowerShell
  4. Windows Command Line Utilities Every Professional Should Know
  5. How To Check System Uptime in Both Windows and Linux Machine

Spread The Knowledge

Leave a Comment