PowerShell

PowerShell for Beginners (Part 2): The Philosophy Verb-Noun

Now, after the first part PowerShell for Beginners (Part 1): The Console and the First Command, we’ll dedicate ourselves to the commands. But before we begin the answer of the first exercise. We also have a brief review of the first part.

All parts of the series can be found here: PowerShell for Beginners (Series)

Review (Part 1)

Exercise

In the first part I asked how to request a confirmation of the Stop-Computer command.

Find out the parameter to ask for confirmation before shutting down the computer.

(Tip: Type Stop-Computer and then press TAB). The solution can be found in the next article, but I think you will find it out yourself 😉 Remember the code of practice!

If you’ve followed my tip then you will find that there’s a parameter called Confirm. Open Windows PowerShell. Enter Stop-Computer. Then type and press TAB. After pressing few times you’ll find the Confirm parameter.


Stop-Computer -Confirm

Unbenannt.JPG

Summary


PowerShell is a professional automation scripting language for Windows and  (coming up soon) Linux. PowerShell can be found by using the Windows Search. Each PowerShell command consists of a verb and a noun.


The Philosophy: Verb-Noun

We now come to the second part of the series. In this section, we’ll focus on the PowerShell cmdlets. In concrete terms, the philosophy of these commands. Each PowerShell command called cmdlets contains a verb and a noun, separated by a hyphen. Remember our first command Stop-Computer. Stop is the verb and Computer is the noun. Here are some examples of this philosophy or naming convention:


Get-Date

Unbenannt.JPG


Get-Process

Unbenannt.JPG


Start-Process 'https:\\sid-500.com'

The last command opens my website. Start-Process starts a process. And if you specify a Website the command opens a web browser and gets you to my site.

What have we learned so far?


Each PowerShell command called cmdlet contains a verb and a noun, separated by a hyphen.


An important PowerShell cmdlet: Get-Command

Which takes me to to the command Get-Command. Get-Command enables you to show all available PowerShell Commands at once. Let’s explore all PowerShell commands.

Type


Get-Command

Unbenannt.JPG

There’s a lot of them. In order to make the whole thing clearer, we can use a special method. For this we need a pipe and the command more. A pipe connects commands. In concrete terms, the pipe takes everything on the left of the pipe and forwards it to the command to the right of the pipe. You can find the pipe on your keyboard. Unfortunately each keyboard is slightly different. Watch for this icon or press ALT + 124.

Unbenannt.JPG

We’ll focus on the pipe later on. For now the purpose of the pipe is to interrupt the output of the screen. Type


Get-Command | more

Unbenannt.JPG

Unbenannt.JPG

At the end of the screen something has happened. There’s a more statement. If you press Enter, the output is extended by one line. If you press the spacebar a whole screen will be jumped.

Now press CTRL + C. This stops Get-Command.

What have we learned so far?


Get-Command shows all available PowerShell commands. With the more function (and a pipe) you can stop rushing all the content through the console window. CTRL+C stops each command.


How to get a command by verb or noun?

I always recommend playing with “get” commands first. Why? You cannot destroy your system. Get Commands only get something, they don’t modify. So, the task for this part is to find all the “get” commands. Remember: Get ist the verb. Let’s search all of the gets using a simple parameter. Type


Get-Command -Verb Get

Unbenannt.JPG

Nice. Found some interesting? I do. Get-Host.


Get-Host

Unbenannt.JPG

Get-Host shows you information about your system, especially of PowerShell. For example you can see your PowerShell version. Mine is 5.1. That was just a quick glance at a particular Get command. Let’s move on with nouns.

To find a command with a specific noun, for example the noun date, run


Get-Command -Noun Date

Unbenannt.JPG

It is no coincidence that I use this noun. Date contains only 2 commands. That doesn’t make it too extensive.

What have we learned so far?


Get Commands cannot destroy your system. To view commands with a particular verb run Get-Command -Verb. To search for specific nouns run Get-Command -Noun. Get-Host shows information about the PowerShell version.


Using TAB

A nice feature is the use of TAB. If you have no clue about the noun, type Get-Command -Noun and press TAB. Unfortunately, this only works with nouns.

TAB

1.JPG

How to list commands using wildcards

Wildcards are placeholders for any value. The wildcard character is usually *. If you are not happy with searching by verb or noun you can simply use the wildcard character. For example, I don’t know the command to restart the computer. Let’s search for command that includes the word restart. Ok, ok I see, you want to search by verb. No, this is not the task for this part. 😉


Get-Command *Restart*

1.JPG

Nice we have found it. It’s Restart-Computer.

Review


Wildcards (*) are placeholders for any value. Above all, they make the search much more flexible. Use TAB for exploring whenever possible.


Combining two commands (Restart computer in an hour)

This is the last part of this blog post. Do you like sleeping? Yes? PowerShell likes it, too. Type Start-Sleep and specify a time span in seconds.


Start-Sleep -Seconds 10

1.JPG

PowerShell goes to sleep. For 10 seconds.

Let’s think ahead. We can now combine two commands. The first one will be Start-Sleep. The second one is Restart-Computer. The goal is that in about one hour the computer should be shut down. For this we need a separator, to separate the two commands from each other. It’s a dotted comma. Type


Start-Sleep -Seconds 3600; Restart-Computer

Unbenannt.JPG

As you can see PowerShell goes to sleep for 3600 seconds. After this period the computer will do a restart. Trust me, it will happen! Provided that you don’t close PowerShell. Planning this in a scheduled task is by all means the better solution. But that’s a  topic for the more advanced blog posts.

Well, we’ve learned how to stop a command. Press CTRL + C to stop sleeping and the schedule of the restart. Or wait and see your computer shutting down after your configured time span.

Review


A dotted comma enables you to enter more than one command. The commands will be executed one after the other. Start-Sleep can help you to schedule the execution of commands or to wait for something for a specific time.


Exercise

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:

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.

See you next time at the article PowerShell for Beginners (Part 3): The Parameters.


Patrick Gruenauer, MVP PowerShell

Categories: PowerShell

Tagged as: ,

11 replies »

  1. Awesome second part! I began the exercise of opening/closing processes in Powershell. I admit it took me a few minutes to catch on lol, but I found it interesting that when you start the process you can input the verb as an abbreviation of the process (calc) but when stop the process you have to input the processes full name (Calculator).

    Very nice lesson! Good practice.

    Liked by 1 person

  2. You tried to make each and every command as simple as possible to remember…Thank you so much…. M following every single line and it’s great way of teaching…

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.