UnkeptBedroom
UnkeptBedroom

Reputation: 31

Trouble Understanding PowerShell "Help Page" Syntax

I hope this question hasn't been asked before, but I'm having trouble understanding a small part of PS Help Syntax. Microsoft's documentation on the subject has it so that optional parameters are encased in square "[]" brackets. The book I'm reading (Learn Windows PowerShell in a Month of Lunches, 3rd Ed.) says the same. The example provided by the book is to run "Get-EventLog" without any parameters, and you'll see that LogName will come up as a prompt. However, when I look at the help page for Get-EventLog, I see that -LogName is indeed in brackets, making it optional. My question is why do you get a prompt for LogName if it was optional to begin with? What step in logic am I missing here? Is "optional" in this sense only referring to the act of literally typing out "-LogName", not the entirety of the parameter (which would include the string) itself?

Upvotes: 1

Views: 95

Answers (1)

Ash
Ash

Reputation: 3246

Welcome to the site. A reasonable question. I think the documentation here covers this better than I could.

The first parameter in the first parameter set of Get-EventLog is LogName. LogName is surrounded by square brackets which means that it's a positional parameter. In other words, specifying the name of the parameter itself is optional as long as it's specified in the correct position.

This basically means that you can specify one of the log names as the first parameter, as a string, and PowerShell will know that first position, Position[0], is where the -LogName parameter is expected.

So you can type this:

Get-EventLog System

And you don't have to write it like this:

Get-EventLog -LogName System

Upvotes: 3

Related Questions