Introduction to PowerShell
Posted by Superadmin on January 28 2023 16:53:05

Introduction to PowerShell

 

 

 

 

Introduction

 

PowerShell is a command-line shell and a scripting language all in one. It was designed as a task engine that uses cmdlets to wrap tasks that people need to do. In PowerShell, you can run commands on local or remote machines. You can do tasks like managing users and automating workflows.

Whether you're part of an operations team or a development team that's adopting DevOps principles, PowerShell can help. You can use it to address various tasks, such as managing cloud resources and continuous integration and continuous delivery (CI/CD). PowerShell offers many helpful commands, but you can expand its capabilities at any time by installing modules.

When you install PowerShell, you can evaluate its features to see if it's a good fit for your team.

Learning objectives

After completing this module, you'll be able to:

Prerequisites

 

 

 

What is PowerShell?

 

PowerShell consists of two parts: a command-line shell and a scripting language. It started out as a framework to automate administrative tasks in Windows. PowerShell has grown into a cross-platform tool that's used for many kinds of tasks.

A command-line shell lacks a graphical interface, where you use a mouse to interact with graphical elements. Instead, you type text commands into a computer console. Here are some of the benefits of using a console:

Features

PowerShell shares some features with traditional shells:

PowerShell differs from a traditional command-line shell in a few ways:

Installation

In this module, you'll practice using PowerShell on your computer. PowerShell is available across platforms, but if you use a computer that runs Linux, macOS, or an older version of Windows, you'll need to install it.

Instructions for installing PowerShell are different for each OS. Before you continue, take a few minutes to install PowerShell or to verify your PowerShell installation. The next unit in this module shows you how to verify your installation.

Windows

If you're running Windows 8 or later, a version of PowerShell called Windows PowerShell should already be installed. This version differs slightly from the most up-to-date PowerShell release, but it works fine for learning purposes.

You can open Windows PowerShell from the Start menu.

Other operating systems

If your computer runs something other than Windows 8 or later, you need to install PowerShell. To find the installation instructions for your OS, see Install various versions of PowerShell.

PowerShell extension for Visual Studio Code

We recommend that you use the PowerShell extension for Visual Studio Code to author your PowerShell scripts and to run the commands in this module. This extension lets you run commands, and also helps you with snippets, code completion, and syntax highlighting.

 

 

 

Exercise - Run your first PowerShell commands

 

 

>$PSVersionTable

 

 

 

 

Your output resembles the following table:

 

 

wing table:

Output
 Name                           Value
 ----                           -----
 PSVersion                      7.1.4
 PSEdition                      Core
 GitCommitId                    7.1.4
 OS                             Linux 5.4.0-1058-azure #60~18.04.1-Ubuntu SMP Tue Aug 31 20:34:4…
 Platform                       Unix
 PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
 PSRemotingProtocolVersion      2.3
 SerializationVersion           1.1.0.1
 WSManStackVersion              3.0

 

 

The output provides information about your PowerShell version, as well as your platform and edition.

For information limited to your version of PowerShell, you can run a modified version of $PSVersionTable.

 

 

 

>$PSVersionTable.PSVersion


Your output now resembles the following table:


Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      1      4  

This output gives you more details about your version of PowerShell.

Running $PSVersionTable results in output that looks like a table, but is actually an object. For this reason, you can use a period (.) to access a specific property, such as PSVersion.

 

 

 

 

Locate commands

 

 

 

cmdlet (pronounced "command-let") is a compiled command. A cmdlet can be developed in .NET or .NET Core and invoked as a command within PowerShell. Thousands of cmdlets are available in your PowerShell installation. The challenge lies in discovering what they are and what they can do for you.

Cmdlets are named according to a verb-noun naming standard. This pattern can help you to understand what they do and how to search for them. It also helps cmdlet developers create consistent names. You can see the list of approved verbs by using the Get-Verb cmdlet. Verbs are organized by activity type and function.

 

 

 

Here's a part of the output from running Get-Verb:

Output
Verb        AliasPrefix Group          Description
----        ----------- -----          -----------
Add         a           Common         Adds a resource to a container, or atta…
Clear       cl          Common         Removes all the resources from a contai…

 

This listing shows the verb and its description. Cmdlet developers should use an approved verb, and also ensure that the verb description fits their cmdlet's function.

Three core cmdlets allow you to delve into what cmdlets exist and what they do:

Locate commands by using Get-Command

 

When you run the Get-Command cmdlet in Power Shell, you get a list of every command that's installed in PowerShell. Because thousands of commands are installed, you need a way to filter the response so you can quickly locate the command that you need.

To filter the list, keep in mind the verb-noun naming standard for cmdlets. For example, in the Get-Random command, Get is the verb and Random is the noun. Use flags to target either the verb or the noun in the command you want. The flag you specify expects a value that's a string. You can add pattern-matching characters to that string to ensure you express that, for example, a flag's value should start or end with a certain string.

These examples show how to use flags to filter a command list:

 

 

Exercise - Locate a command

 

Locate commands by running the Get-Command cmdlet. The cmdlet helps you search all of the cmdlets installed on your system. Use flags to narrow down your search results to just the cmdlets that fit your scenario.

In this scenario, you're looking for a cmdlet that can help you work with files.

 

  1. Run the command Get-Command with the flag -Noun. Specify File* to find anything related to files.

    PowerShell
     Get-Command -Noun File*
    

    The response shows something similar to the following text:

    Output
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Cmdlet          Get-FileHash                                       7.0.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Out-File                                           7.0.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Unblock-File                                       7.0.0.0    Microsoft.PowerShell.Utility
    

    The cmdlets Get-FileHashOut-File, and Unblock-File all match your query. Now, you have a manageable response. To further filter the response, add the -Verb parameter to your query.

  2. Run Get-Command. Specify the flags -Verb and -Noun.

    PowerShell
    Get-Command -Verb Get -Noun File*
    

    The result is similar to the following output:

    Output
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Cmdlet          Get-FileHash                                       7.0.0.0    Microsoft.PowerShell.Utility
    

    This time, only one record matches your search, because you specified both the -Noun parameter and the -Verb parameter.

Because the domain you work in is file management, you specified File as the noun. If you know what you want to do within that domain, you can specify -Verb parameters. By using one or possibly two parameters, you can quickly find the cmdlet you need.

 

 

 

Knowledge check

 

 

Choose the best response for each question, then select Check your answers.

 

1. What's a correct way to locate a command in PowerShell?
Call Get-Command 'name of command'

Call Find 'name of command'
Call Locate 'name of command'
2. How would you search for commands that deal with files?
Call Get-Command -Verb File*
Call Get-Command -Noun File
Call Get-Command -Noun File*

Summary

In this module, you started by learning what PowerShell is and what you can use it for. You explored its primary features, and how it differs from traditional shells like Git Bash. You also looked at how to install PowerShell, and briefly explored authoring it by using Visual Studio Code and an extension.

You then learned about compiled commands called cmdlets. You looked specifically at a command called Get-Command that helps you locate the command you need.

You should now have a good understanding of PowerShell, what it's used for, and how to use its commands efficiently.

Resources