Now, after the second part PowerShell for Beginners (Part 2): The Philosophy Verb-Noun, we’ll dedicate ourselves to the PowerShell parameters. But before we begin the answer of the second exercise. We also have a brief review of the second part.
All parts of the series can be found here: PowerShell for Beginners (Series)
Review (Part 2)
In the second part I asked how to stop a process with PowerShell.
Find out the commands to do something with processes. I mean Windows processes. Use Get-Command (search for nouns with wildcards or TAB) to find all of them. Then open Notepad. Try to close this notepad process with PowerShell.
Well, the first part is to search for all commands related to processes. There are many ways to do this. I’ll show you two of them.
Get-Command -Noun Process
This would also work:
Get-Command -Noun Proc*
The first one is more clear. Maybe you have found another way. Do not hesitate to post this as a commentary.
The next part is to open notepad. You can do this by searching notepad graphically or simply open notepad in PowerShell.
Well, as we have seen there’s a command called Stop-Process. This sounds good. Let’s stop notepad with the command below.
Stop-Process -Name notepad
Well I have to admit that that was not as easy as it looks like. Some of you may have skipped the name parameter. This is not allowed. The parameter Name for the cmdlet Get-Process is a mandatory named parameter. This brings me to our topic for this third part of the series: Parameter.
Each PowerShell command called cmdlet contains a verb and a noun, separated by a hyphen. Get-Command shows all available PowerShell commands. Wildcards (*) are placeholders for any value. Use TAB for exploring whenever possible.
Fundamentals of Parameters
Mandatory Parameters (Required)
Let’s stick to the above example. How do I know that this parameter is a named and mandatory parameter? I’m good in reading the help. Type
Get-Help Stop-Process -Parameter Name
As we can see, this parameter is required and named. What does this actually mean?
- Required: means that this parameter is mandatory. So you have to specify the name of the process
- Named: signifies that you have to type the parameter name and the argument (in our case notepad). Everything else leads to an error message
Which brings me to the next example. Most of PowerShell parameters are not mandatory, nor named. Some of them are positional parameters. Let’s take a look to this second example that shows the parameter LogName of the Get-EventLog cmdlet.
Get-Help Get-Eventlog -Parameter LogName
Crazy thing. It’s required but it’s position is not named, but 0. This means that you have to specify the LogName, but you can do it without the parameter name. The first argument that is given will be automatically bound to this parameter.. Let’s give it a try. PowerShell is asking me for the LogName, because LogName is a mandatory parameter. (Required? true)
There are 3 main types of Windows Event Logs: System, Application and Security. What happens if I enter one of the three without the parameter name? It works because the parameter is not named.
What if a Parameter does not require an argument? I mean parameters without any input, but with default values. They are called switched parameters. Let’s say you want to show what will happen if you restart your computer. (We’ve already done this before). The parameter for this is WhatIf.
Get-Help Restart-Computer -Parameter WhatIf
Now let’s compare the WhatIf parameter with another parameter which calls for an argument, for example a string. Note the red line. Unlike WhatIf, ComputerName requires a string.
Get-Help Restart-Computer -Parameter ComputerName
WhatIf requires no input. ComputerName calls for an HostName. This means that you can run the WhatIf parameter without any input. Look at this:
But we encounter problems when we try to restart a remote computer without specifying it’s name.
It’s not possible to cover all topics of parameters in a single blog post. You can start exploring all parameters by using the following command:
Get-Help Get-Eventlog -Parameter *
What have we learned so far?
Parameters are different. There are required, positional, switched, mandatory and not mandatory parameters. Get-Help can help you exploring all parameters of a command. To do this run Get-Help Your-Command -Parameter *. Use TAB whenever possible.
Some useful parameter examples to play with
At the end I would like to provide some useful examples you can play with. Don’t worry if the commands seems very complex to you. It’s ok. We haven’t done some things yet. They’re just for playing.
Retrieve the newest 5 Event Log Errors from the System Log
Get-EventLog -LogName System -Newest 5 -EntryType Error | Format-Table Time,EntryType,Message -AutoSize -Wrap
Get the Filename of a Windows Process
For this notepad should be already started.
Get-Process notepad -FileVersionInfo
Show all *.txt files in C:\Temp
Get-ChildItem is similar to dir, but much more powerful. The command below shows all txt files in C:\Temp.
The parameters can also be omitted.
Get-Childitem C:\Temp *.txt
Remember our Code of Practice:
Do it yourself. Anything you don’t do yourself will soon be forgotten.
The exercise up to the next part is:
Why does this command not work?
See you next time at the article PowerShell for Beginners (Part 4): The PowerShell Help (Get-Help)
Patrick Gruenauer, MVP PowerShell
I started notepad by using “start-process notepad.exe” and then I used “stop-process” andpressed tab and was given a process id. So I then used “get-process” which gave me a long list so I followed it up “get-process notepad” I could tab through the running process names . I was presented with three notepad processes on my computer. So I used the largest numbered one: stop-process 14044 and notepad closed.
Thank you very much for these lessons.