Introduction to PowerShell
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.
After completing this module, you'll be able to:
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:
PowerShell shares some features with traditional shells:
cls
(clear the screen) and ls
(list the files). Therefore, new users can use their knowledge of other frameworks and don't necessarily have to remember the PowerShell name for familiar commands.PowerShell differs from a traditional command-line shell in a few ways:
It operates on objects over text. In a command-line shell, you have to run scripts whose output and input might differ, so you end up spending time formatting the output and extracting the data you need. By contrast, in PowerShell you use objects as input and output. That means you spend less time formatting and extracting.
It has cmdlets. Commands in PowerShell are called cmdlets (pronounced commandlets). Unlike many other shell environments, in PowerShell cmdlets are built on a common runtime rather than separate executables. This characteristic provides a consistent experience in parameter parsing and pipeline behavior.
Cmdlets typically take object input and return objects. The core cmdlets in PowerShell are built in .NET Core, and are open source. You can extend PowerShell by using more cmdlets, scripts, and functions from the community and other sources, or you can build your own cmdlets in .NET Core or PowerShell.
It has many types of commands. Commands in PowerShell can be native executables, cmdlets, functions, scripts, or aliases. Every command you run belongs to one of these types. The words command and cmdlet are often used interchangeably, because a cmdlet is a type of command.
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.
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.
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.
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.
>$PSVersionTable
Your output resembles the following table:
wing table:
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
.
A 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
:
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:
Get-Command
cmdlet lists all of the available cmdlets on your system. Filter the list to quickly find the command you need.Get-Help
core cmdlet to invoke a built-in help system. You can also run an alias help
command to invoke Get-Help
but improve the reading experience by paginating the response.Get-Member
core cmdlet to drill down into that response and learn more about it.
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:
-Noun: The -Noun
flag targets the part of the command name that's related to the noun. That is, it targets everything after the hyphen (-
). Here's a typical search for a command name:
Get-Command -Noun a-noun*
This command searches for all cmdlets whose noun part starts with a-noun
.
-Verb: The -Verb
flag targets the part of the command name that's related to the verb. You can combine the -Noun
flag and the -Verb
flag to create an even more detailed search query and type. Here's an example:
Get-Command -Verb Get -Noun a-noun*
Now you've narrowed the search to specify that the verb part needs to match Get
, and the noun part needs to match a-noun
.
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.
Run the command Get-Command
with the flag -Noun
. Specify File*
to find anything related to files.
Get-Command -Noun File*
The response shows something similar to the following text:
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-FileHash
, Out-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.
Run Get-Command
. Specify the flags -Verb
and -Noun
.
Get-Command -Verb Get -Noun File*
The result is similar to the following 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.
Choose the best response for each question, then select Check your answers.
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.