Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Introduction to PowerShell
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:
- Understand what PowerShell is and what you can use it for
- Use commands to automate tasks
Prerequisites
- Basic familiarity with using a command-line shell like Command Prompt or Git Bash
- Visual Studio Code installed
- Ability to install Visual Studio Code extensions
- Ability to install software on your computer, if you're not using a Windows operating system
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:
- Interacting with a console is often faster than using a graphical interface.
- In a console, you can run batches of commands, so it's ideal for task automation for continuous-integration pipelines.
- You can use a console to interact with cloud resources and other resources.
- You can store commands and scripts in a text file and use a source-control system. This is probably one of the biggest benefits, because your commands are repeatable and auditable. In many systems, especially government systems, everything must be traced and evaluated, or audited. Audits cover everything from database changes to changes done by a script.
Features
PowerShell shares some features with traditional shells:
- Built-in help system: Most shells have some kind of help system, in which you can learn more about a command. For example, you can learn what the command does and what parameters it supports. The help system in PowerShell provides information about commands and also integrates with online help articles.
- Pipeline: Traditional shells use a pipeline to run many commands sequentially. The output of one command is the input for the next command. PowerShell implements this concept like traditional shells, but it differs because it operates on objects over text. You'll learn more about this feature later in this module.
- Aliases: Aliases are alternate names that can be used to run commands. PowerShell supports the use of common aliases such as
cls
(clear the screen) andls
(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.
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:
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
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: The
Get-Command
cmdlet lists all of the available cmdlets on your system. Filter the list to quickly find the command you need. - Get-Help: Run the
Get-Help
core cmdlet to invoke a built-in help system. You can also run an aliashelp
command to invokeGet-Help
but improve the reading experience by paginating the response. - Get-Member: When you call a command, the response is an object that contains many properties. Run the
Get-Member
core cmdlet to drill down into that response and learn more about it.
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:
-
-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:PowerShellGet-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:PowerShellGet-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 matcha-noun
.
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.
-
Run the command
Get-Command
with the flag-Noun
. SpecifyFile*
to find anything related to files.PowerShellGet-Command -Noun File*
The response shows something similar to the following text:
OutputCommandType 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
, andUnblock-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
.PowerShellGet-Command -Verb Get -Noun File*
The result is similar to the following output:
OutputCommandType 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.
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.