PowerShell Basics: Parameters

In this short guide we are going to learn what PowerShell parameters are as well as how to use them from our PowerShell console. Let’s get started!

What We'll Learn:

Welcome!
This short guide is all about PowerShell parameters.

  • We will start by learning what exactly parameters are.
  • We will then learn how parameters are structured on a command, and what their syntax is.
  • And finally, how to get information on what parameters a cmdlet has, and how they can be used.

Lets get started!

What are PowerShell Parameters?

Parameters in PowerShell, also known as arguments, options or switches, help with the passage of arguments on a script or a cmdlet or allow users to provide some sort of input. Most cmdlets, functions, and scripts rely on parameters to allow users do just that.
We can use parameters with Cmdlets for example, to make them alter their function in some way, or to perform a specific task.

Parameter Syntax:

Now that we now what parameters are let’s start using them. Parameters follow cmdlets, functions or scripts and have the following syntax:

-Parameter Value

Or

-Parameter:Value

As you can see, the name of the parameter is preceded by a hyphen (-), which signals to PowerShell that the word following the hyphen is a parameter.
Afterwards we have the value of the parameter which is separated with either a space or a colon

Parameter Examples:

To better understand parameters let’s have a look at a simple example. Let’s say that we want to view the name of every file and folder in our desktop directory.
Here is the command we are going to use:

Get-ChildItem -Path C:\Users\Julian\Desktop

Let’s break it down. In this example, Get-ChildItem is the cmdlet, -Path is the parameter, and C:\Users\Julian\Desktop is the value of the parameter.
Upon running the command, you can see that every file that’s located in our desktop is immediately shown.

PowerShell Parameters Arguments Options

Note, that the above syntax will only work for parameter values that do not have spaces within them.
For values that do contain spaces such as a path, we need to surround the value of our parameter in quotes.
The quotes indicate to the console that the spaces separating the words do not constitute an additional parameter and are just a continuation of our value. Here is an example:

Get-ChildItem -Path “C:\Users\Julian\Desktop\My Folder”

Quotes are not necessary only for parameter values but for any path or name that contains a space within it.
Some parameters do not require or even accept values, and they can be used in a similar way to a command line switch. Such as this example:

Get-ChildItem -Path C:\Users\Julian\Desktop -File

The -File parameter in this case does not need a value and its used to instruct our cmdlet to only display files.
For some parameters, the opposite is true. Meaning that the name of the parameter is not mandatory, and just specifying its value is enough.
For example, we can execute the following command without getting any errors even though we have not specified the -path parameter.

Get-ChildItem C:\Users\Julian\Desktop -File

This happens because the console sees that our value is a path, and thus correctly assumes that we have provided a value or the path parameter

You cant always type only a value and expect the PowerShell console to understand the implied parameter however, because some parameters might accept the same type of value and the console might not know which one you are referring to.
Learning when and when not the name of a parameter is necessary will come naturally to you the more you use and practice on your PowerShell console, so don’t stress too much over it.

Discovering Parameters:

Cmdlets can have a lot of parameters and remembering all of them along with their appropriate syntax is impossible, that’s where the Get-Help cmdlet comes in.
As the name implies, Get-Help displays valuable information about a cmdlet such as what is function is, how it can be used and what its parameters are.
To use it simply type get-Help followed by the name of a cmdlet.

Get-Help Get-Childitem

If you get an error about help files not being installed run the update-help cmdlet to update your help files.

Update-Help

This operation can take a while to complete.
The Update-Help cmdlet might occasionally through an error because for some of the URLs from which it downloads the help files might be invalid, if that the case for you, simply ignore the error, the majority of your help files will be installed regardless.

Now that the operation is over, upon running the get-help command once more, you should see a detailed description of the specified cmdlet.

Discovering and Using Cmdlets Get-Command Get-Help

 We can get even more information by using the -detailed parameter or we can view usage examples using -examples.

Get-Help Get-ChildItem -detailed
Get-Help Get-ChildItem -examples

Using this information, you will be able to accomplish any task you wish without losing your head over it.

Summary:

  • Parameters are words, that can be typed after a cmdlet to make it function in a specific way.
  • Values that contain spaces within them, need to be surrounded in quotes, so that the console knows that the words after the spaces do not constitute additional parameters.
  • To get more information on what parameters a cmdlet has and how they can be used, make use of the Get-Help cmdlet.

That's It!

You now know what parameters are and how they can be used.

If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows PowerShell.

Windows PowerShell

Dont Stop Learning!

Windows PowerShell Course

Learn the ins and outs of Windows PowerShell along with PowerShell scripting and become an expert!

Read More:

Comments:

Leave a Reply

Your email address will not be published.

Take Your Skills to the next level!

Become one of our beloved students by enrolling into one of our courses. Get started now!