Command Syntax Part 2

Sign up to access this lesson

Click here to sign up and get access to this lesson!

Saving Progress...

How do you tell which Parameters are optional and which arguments are required?

To answer that question we need PowerShell’s Help system

Type help get-service -showwindow

Scroll down to the Syntax section and what I have done is I’ve copied the Syntax section into

my scripting Pane. So you can use show window and I’ll view the syntax from the scripting

pane.

Now watch this I’m going to type get-service, notice that this cmdlet runs without adding

any parameters.

Now check this out type get-eventlog press return, notice what it says:

So, the question is why did get-service run when get-eventlog required a value for the -logname parameter?

1 The answer is in the syntax of the help files for get-service and get-eventlog

Lets checkout get-service first. Let’s start with parameter set #2 and set #3

Optional [Parameter Argument] Notice that all the parameters and the arguments are

surrounded by square brackets.

This means that adding the parameters are optional and not needed. So,

2 Get-service will run without adding any parameters.

Now let’s take a look at the syntax for get-eventlog

If you want to follow along type help get-eventlog -showwindow

Again I’ve copied the syntax for get-eventlog to my scripting pane

Let’s go through the syntax, notice that almost every parameter and every argument are

surrounded by square brackets, that means that they are all optional or not needed.

Notice -logname is surrounded by square brackets but the argument is not.

Required Argument

That means, because there are square brackets around the parameter -logname, the name

logname is optional but the argument <string> is required.

That is why when you ran get-eventlog without any parameters, PowerShell asks for a

value for -logname

Now go back to get-eventlog, logname:

type Application and press return, and the command runs.

3 Let’s go ahead and clear the screen cls

4 Positional parameters [Param] (Use get-eventlog scroll down to #Position)

Type help get-eventlog -showwindow

Scroll down until you see the parameter attributes list. We are going to be working with

three parameters from this list, -InstanceID, -logname and -newest

From the list notice that the parameter -logname has a position of 0,

-InstanceID has a position of 1 and -newest has a position of named.

Now what does this mean?

0, 1 and name, refer to the actual position that the cmdlet must be placed in the order of

the cmdlets.

So -logname is positional, it’s position is 0 which is the first position.

The parameter -instanceID position is 1 which is after -logname in the order of cmdlets

Ok let's check that out

So type get-eventlog application 0,1

(I don’t have to type the parameter -logname or the parameter -InstanceID, because they

are both optional because they're surrounded by square brackets

Now take a look at the argument for -InstanceID because there are two square brackets

within the angle brackets the parameter -InstanceID can take multiple arguments. Now

press return and that runs.

Now let's see if we can move these values out of order. Move the 0,1 in front of

application and press return.

And we get an error because Powershell expects the positional parameter -logname or the

value type application to be 5 the first in the list.

6 Named Parameters

Take a look at -Newest, notice that the position is named.

Named means that you can put -newest anywhere in the order of parameters and it will

work. Let’s check it out

Type get-eventlog -newest 5 application 0,1 and that worked.

We see that we moved the parameter with a position called named and moved that in front

of the positional parameter -logname and we see that the command ran

Sign up to access the rest of this lesson

You must either log in or sign up to access this lesson.

Saving Progress...

0 0 votes
Lesson Rating
Subscribe
Notify of
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
profile avatar
Ambrose Garza(@ambroseg)
Member
Points: 4122
3 months ago

get- service has a required parameter of -DisplayName. But runs without it. It seems to me that the only true required parameters are the ones that are not “named” in the positional setting and the “named” ones are ones that can be left off.

example get-help get-help -showwindow it shows that -full, -showwindow and few others are required but are also “named” in postional.

is this correct or did i make a mistake

Last edited 3 months ago by Ambrose Garza
profile avatar
Ricardo P(@ricardop)
Admin
Power Student
Points: 16154
Reply to  Ambrose Garza
3 months ago

Hi profile avatar Ambrose Garza
The get-service runs on its own since it gets all the services on the computer if not using -DisplayName.
I do see what you mean. When executing get-help get-service -showwindow just -DisplayName is required.
I can’t seem t find why some are required and why others don’t.
It seems that if you use a parameter you need to specify a value.
Ricardo

Administration and Automation with Windows PowerShell

0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 42min

0 / 7 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete