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.
Types of PowerShell Parameters:
Windows PowerShell has three types of parameters:
- Optional parameter
- Mandatory parameter
- Positional 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.
- Execute the following two cmdlets and observe the output.
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.
Step 2: Get-Help -Name Get-Command can be executed as Get-Help Get-Command as -Name is enclosed with square brackets.
Step 3: Since –Verb(Switch) in the parameter is not enclosed within square brackets, the switch becomes mandatory.
Step 4: Get-Command Get throws an error because -Verb is not specified.
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 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.
Step 2: The -LogName parameter need not be specified as shown in the below example.
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.
- -LiteralPath specifies the source and –Destination specifies where the value has to be copied.
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.
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.
Step 3: In the below example, contents of File2 is copied to File1.
Source : Microsoft
- Transfer Files Between Windows & Linux Machine Using PowerShell
- An Introduction to VMware PowerCLI
- Disable Clipboard Redirection Multiple Servers PowerShell
- Windows Command Line Utilities Every Professional Should Know
- How To Check System Uptime in Both Windows and Linux Machine