Windows PowerShell Tutorial 2 - Cmdlets

The Cmdlet: What it is, how to get information on it, and how to use



  1. Regedit32
    PowerShell as a Scripting language comes with built in Commands you can use. An individual command in PowerShell is commonly called a Cmdlet (pronounced command-let).

    The Cmdlet is one of the many useful tools made available for supporting Scripting in PowerShell which is based around the .Net Framework.

    It is perhaps one of the most basic aspects of PowerShell, yet can be somewhat daunting at the same if you are not familiar with them. When PowerShell was first released there was just over 100 purpose built Cmdlets included, but today, in Windows 10, PowerShell Version 5.1 comes with 527 Cmdlets built in ready and waiting for you to use, and that list is continuing to grow.

    The individual names for each Cmdlet is made up of a verb and a noun which describes the action to be taken, and on what item this action will occur. Each Cmdlets name is in the format verb-noun separated with a hyphen, for example Set-Location is a Cmdlet that can set the location you will work in. For those familiar with DOS, and the Command Prompt, Set-Location is the same as CD (Change Directory).

    Example image to illustrate this:

    PS2.png

    Note: Just as in DOS or Command Prompt, you do not always need to use speech marks around the path you wish to change location to. You typically only need to use speech marks when the path contains a space, for example: C:\Program Files (x86) contains spaces, and this it is good practice to place speech marks around this if changing the location to this [ i.e. Set-Location "C:\Program Files (x86)" ]

    If you completed the task at the end of Tutorial 1, you would have made use of the Cmdlet Write-Host and a parameter you passed through it $env:ComputerName

    This Cmdlet instructs the System to write a message to the Host, which in this case was the PowerShell console. The parameter $env:ComputerName is what the Cmdlet Write-Host was to write to the console. This would have displayed the Name you chose to give your Computer when you installed Windows onto it.

    Again, you do not necessarily require the use of speech marks around the parameters, but its a good idea to get into the habit of using them all the same, so the full command and parameter is best typed as Write-Host "$env:ComputerName".

    PowerShell comes with a Help database, but this is continually being updated. and the initial Help database is a little limited, so its a good idea to choose to update it ASAP, as you can learn a lot about the Commands and how to use them from the Help files.

    To update the Help open PowerShell and in the console type the following command:

    Code (PowerShell):
    Update-Help
    Press Enter key to execute. You may be prompted to confirm you wish to download the Help database updates. If you are type Y and press Enter key.

    The update will take a minute or two given on top of the 527 Cmdlets there are also Functions and Modules to update [ More on those in a future tutorial ].

    PS3.png

    Note: You can also update the Help files using either Windows PowerShell (Admin), or Windows PowerShell ISE.

    Now that we have updated the Help File database, we are ready to find out what Commands are available can can easily get help using them.

    In a PowerShell console type the following to get a full list of all built-in commands which include the 527 Cmdlets.

    Code (PowerShell):
    Get-Command
    Press Enter key to execute.

    PS4.png

    As you can see its quite a massive list. All the Cmdlets are listed at the end.

    I'll discuss the Alias and Functions in a later tutorial in more depth, but briefly, an Alias is a way we can shorten the full command name, so for example, rather than typing Set-Location "C:\Windows" we can instead type SL "C:\Windows"

    PS5.png

    While this is handy, its not a great idea in my view, as given how many commands there are in PowerShell it is easy to forget what that shorthand stands for; thus if you have a bug in your Script you might find it hard to figure out why if you have forgotten was SL stands for, for example. However, its perfectly fine to use shorthand and assuming you made no errors in your coding the Script will run just fine.

    Functions are as you'd expect them to be, that is a series usually of more than one complex command, which can be used later in a console to perform specific tasks. In fact Functions are quite similar to an Alias, but the cool thing is you can store Functions in a PowerShell Profile, which allows them to load as you start PowerShell, which means you only ever have to code the Function once, and can use it over and over again with multiple Scripts. More on this later in a future tutorial.

    As you scroll that long list of Commands (in particular the 527 Cmdlets) you might think to yourself, 'What on earth do I do with all these'? That is when you'll remember you just downloaded updates to the built-in Help file database in PowerShell, which is available to you in the PowerShell console or in the PowerShell ISE console.

    You guessed it, PowerShell has a Cmdlet to get help.

    Type Get-Help and press Enter key

    PS6.png

    The first screen gives you tips on how to use Help, i.e. what parameters you can pass to the Cmdlet Get-Help.

    To get help on a specific Cmdlet we pass the parameter -Name through the Cmdlet Get-Help then type the name of the Cmdlet we want help about.

    For example to get help about the Cmdlet Set-Location type the following:

    Code (PowerShell):
    Get-Help -Name Set-Location
    Press Enter key to execute.

    PS7.png

    You should be beginning to see just how awesome PowerShell is now. Microsoft have put a lot of thought into this new Scripting language, given its intended to replace
    VB Scripting and ultimately will replace the Command Prompt too. Plus, given it's now open source, there literally are thousands of keen programmers contributing to its development and freely offering custom modules and Functions for use.

    If you want even more Help on the Cmdlet's then add one more parameter at the end of your command, like so:

    Example:

    Code (PowerShell):
    Get-Help -Name Set-Location -Full


    Press Enter key to execute

    The final parameter -Full is what is known as a Switch parameter. It sets a condition to the command and itself takes no additional details, unlike -Name which obviously needed to be told which Cmdlet — in this case Set-Location — to get help on.

    Using the switch parameter -Full provides you not only the initial help you saw in previous image, but continues to literally give you a full explanation about all the Parameters you can use with the Cmdlet. Try it and you'll see what I mean.

    Do you have a text file you want to read in your console? Perhaps a DISM log?

    No problems, there is a Cmdlet for this!
    • First, Set-Location to where the text file is
    • Second, make use of the Cmdlet Get-Content to view the text in your console
    Sample image to illustrate this

    PS8.png

    Note: You can do all this in a single command like so if you wish:

    Code (PowerShell):
    Get-Content -Path "$env:WinDir\Logs\DISM\Dism.log"
    You can see how making use of passing the parameter -Path through the Cmdlet saved having to type Set-Location "$env:WinDir\Logs\DISM"


    Important point!

    Before you go, I purposely left this point to last to make sure it sticks in your mind. Take a look at the following image:

    PS9.png

    Earlier I explained how you can use the switch parameter -Full to get a full description on a Cmdlet from the Help File database.

    Above is the Full information on the Cmdlet Set-Location and in particular I want you to notice that each available parameter for a Cmdlet has a Boolean value Required? which as you can see in the example above is set to either True or False. Why this difference?

    If the Required? value is True then
    you must use this parameter with the Cmdlet!

    If on the other hand the Required? value is False then you do not have to use the parameter as it is optional whether you choose to use it with the Cmdlet,


    There is literally so much more I could write here, but for now let's take a breather and I'll let you investigate all the Cmdlets and the Help files on them so you can begin to come to grips with just how powerful PowerShell will prove to be.

    In the next tutorial I'll give a brief explanation on variables.


    Regards,

    Regedit32
    ClickOK and Ian like this.