Learn and use PowerShell with just three commands

PowerShell Basics

Before we dive into the three commands you’ll need to memorize to start using PowerShell, we’ll cover a few basics.

Note: If you’d like to follow along (you should!) you’ll need access to PowerShell. If you’re on a Windows device, PowerShell is already included, simply launch it. Linux users can install PowerShell from here and MacOS users can install from here.

PowerShell is a command-line shell and scripting language that aims to help administrators and power-users rapidly automate tasks.

To accomplish this, PowerShell adheres to design goals that enhance discoverability and consistency. To put it simply: PowerShell is easy to pick up and start using. It’s designed for flawed people like you and I to accomplish great things.

Cmdlets

PowerShell commands are referred to as cmdlet’s. Cmdlets perform an action and typically return an object to the pipeline.

Try it yourself! Launch a PowerShell console window and type:

Get-TimeZone

Congrats! You just ran your first cmdlet! This cmdlets returns an object containing data about your device’s currently configured time zone.

Verb-Noun

In PowerShell, cmdlets follow a verb-noun structure to help improve accessibility, and reduce command memorization.

This means that cmdlets follow a DoAThing structure.

In the above example you were Geting-theTimeZone.

Because we know about the Verb-Noun structure, and have previously seen the Get-TimeZone cmdlet, we can infer that there might also be a Set-TimeZone (which does indeed exist). Without any prior knowledge of these cmdlets, we can further infer that Get-TimeZone will be read-only while Set-TimeZone will take some type of action adjusting the devices time zone settings.

A list of approved PowerShell Verbs is available, and all properly formed cmdlets adhere to this guideline.

Video

If you prefer video format over written documentation I discuss this topic in the following TechThoughts video:

Three commands needed to learn and use PowerShell

Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime.

There are thousands of cmdlets. It is impossible for you to memorize them all. You don’t have to. You only have to memorize three. With these three cmdlets, you can find, explore, and leverage the others to perform a wide variety of tasks in your organization.

Get-Command

Get-Command is a cmdlet that can be used to display available commands.

In fact, it can identify every cmdlet available to run on your system. Try it:

Get-Command *
# An asterisk (*) in many languages acts as a wildcard. This syntax is saying: get me ALL of the commands

Using just a single wild card will display all available commands on the system. Often though, you’ll be after a specific objective. Lets try seeing if there is a cmdlet for dealing with system processes:

Get-Command *process*
# the wild cards around process will find ANY command that contains the word process

This reveals all cmdlets that deal with processes. Unsurprisingly, we find the approved verb structure of:

  • Get-Process
  • Start-Process
  • Stop-Process
  • Wait-Process

Get-Help

Wait, what do all of those cmdlets do? Does Stop-Process do what I actually think it does?

This is where Get-Help comes in. It provides information about what a cmdlet does, and how it can be used.

Before we leverage Get-Help, it’s a good idea to first run Update-Help. This cmdlet will download up-to-date help information for all of your cmdlets. For Windows users, you will need to launch an elevated PowerShell prompt as this command requires administrative privileges to update the help files.

Note: Linux and MacOS users, Update-Help isn’t working as intended for you at this time so you will run slightly different commands in these short examples.

With your help up-to-date you can now try Get-Help:

# Windows Users:
Get-Help Stop-Process
#Linux/MacOs Users
Get-Help Stop-Process -Online
Learning to use PowerShell with Get-Help

As you can see we get a breakdown of exactly what the cmdlet does. Note that at the bottom of the return that that we can also call the same command with different parameters such as -examples

Lets try that now:

Get-Help Stop-Process -Examples
Example 1: Stop all instances of a process
    PS C:\Stop-Process -Name "notepad"
    This command stops all instances of the Notepad process on the computer. Each instance of Notepad runs in its own process.
    It uses the Name parameter to specify the processes, all of which have the same name. If you were to use the Id parameter
    to stop the same processes, you would have to list the process IDs of each instance of Notepad.

Note that this provides detailed examples of how to run the Stop-Process cmdlet, and clearly outlines what you should expect as a result.

Armed with Get-Help you can teach yourself how to use any cmdlet on your system!

Get-Member

As previously mentioned, PowerShell deals with objects. Get-Member serves to help you identify what type of objects you are dealing with when using PowerShell. It can also help you identify what properties and methods are available to that object.

Try the following, don’t worry about the | for now, we’ll cover that in a later lesson:

Get-Date | Get-Member
TypeName: System.DateTime
Date                 Property       datetime Date {get;}
Day                  Property       int Day {get;}
DayOfWeek            Property       System.DayOfWeek DayOfWeek {get;}
DayOfYear            Property       int DayOfYear {get;}
Hour                 Property       int Hour {get;}
Kind                 Property       System.DateTimeKind Kind {get;}
Millisecond          Property       int Millisecond {get;}
Minute               Property       int Minute {get;}
Month                Property       int Month {get;}
Second               Property       int Second {get;}
Ticks                Property       long Ticks {get;}
TimeOfDay            Property       timespan TimeOfDay {get;}
Year                 Property       int Year {get;}

This indicates that Get-Date returns a System.DateTime object which contains many properties, such as Date, Day, Hour, Second, etc. This is a complex object return that contains a lot of data!

Note: You may notice that if you simply run Get-Date that many of these properties are not returned to you. They are still there, but only a subset of properties is displayed to you from certain cmdlets. Try the following on your computer and see if it aligns with Get-Member:

Get-Date | Format-List

Now lets look at a more simple example:

Get-Random | Get-Member

This returns only a simple object of TypeName: System.Int32

Get-Random is just a quick cmdlet to get a random set of numbers. It has no other properties and only returns a System.Int32 number.

Understanding what type of object you are working with is very important in PowerShell. Get-Member is the cmdlet you’ll use to check if you aren’t sure!

When Get-Command doesn’t do the job

Sometimes the noun you are searching for might not be as apparent as you might like. For example, imagine you want to get some information about a file on your system. You might infer something like Get-File, and even try something like:

Get-Command *file*

What you’ll quickly find though, is that there is not a PowerShell command that gets a lot of file information that contains the word file. In these cases you’ll have to fall back to an internet search, and a quick Google reveals that Get-Item is the cmdlet that does what we need.

Bonus fourth command

Find-Module

PowerShell can’t do everything. Or can it?

Did you know that native PowerShell can’t send Telegram messages? PowerShell supports modules though, and someone out there may have just written a module that enables PowerShell to send Telegram messages.

This is where Find-Module comes in. It can search the PowerShell Gallery and find modules that add additional capabilities to PowerShell.

Try this out:

Find-Module -Tag Telegram

Chances are good that if you can think of it – someone has written a module for it. Find-Module helps you locate modules that extend the power of PowerShell!

How might you go about getting these modules? With the three commands explored above, you now have everything you need to figure that out!

Series Navigation<< PS1 – Should you learn PowerShell?Working with the PowerShell Pipeline >>

6 Responses

  1. This is the best blog ever !! Everything ranging from text an code-blocks to images, videos and links is so perfectly documented !!
    Thanks a lot for this blog !!

  2. Arty says:

    The best blog I have ever found to learn powershell

  3. murphy says:

    You made up my mind for me, to learn and perfect PowerShell as language with your simplified presentation.
    Thanks

  4. George Armenante says:

    Thank you for theses videos.

    As a beginning learner…. I really appreciate your teaching style. Also, the written notes are excellent because…. when I want to go over a lesson’s contents it is easy to scan your notes… much quicker than replaying the video.

  5. Alex says:

    Thank you for your blog and videos. you understand how to teach better than most people. your powershell lessons are the best so far.

  6. Augustine says:

    Thank you so much for your content. I am a beginner, I just started to learn cybersecurity and powershell is one of the requirements. This is the first time I heard the name and your video has cleared some of the confusion and boosted my confidence.

Leave a Reply

Your email address will not be published. Required fields are marked *