Introduction
PowerShell is an open-source command-line shell and scripting language. It is built upon the
.Net Core platform and is used heavily to manage workloads in Azure. It is cross-platform
meaning it will run on Linux and Windows. It is increasingly becoming adopted by systems
administrators to manage on-premise and Cloud workloads.
In this lab, you will learn how to navigate the PowerShell console and create a PowerShell script.
Instructions
1. To open the lab IDE, in the top-left corner of the lab, wait for the setup to complete and click
Open Development Env.:
2. Click the Terminal menu and choose New Terminal:
A PowerShell terminal will appear.
3. Input the following command:
Copy code
1
$psversiontable
This will display the current version of PowerShell. In this lab you are using version 7.1.1 of
PowerShell Core:
Newer PowerShell commands are not available on older versions of PowerShell, this is why it's
important to be able to tell which version of PowerShell you're working with or writing a script
for.
4. Use Get-Command inside the terminal to retrieve a list of all commands that contain the word
"process":
Copy code
1
Get-Command -Name *process*
PowerShell commands follow a <Verb>-<Noun> format such as "Get-Process" or "New-Item".
Each command is referred to as a cmdlet (command-let). The Get-Command cmdlet retrieves a
list of available commands within PowerShell while -Name is called a parameter and enables
Get-Command to retrieve commands based on the cmdlet name. In this example, you are using a
wildcard search by inserting a * before and after the word process to retrieve a list of all the
commands in PowerShell that contain the word "process":
5. Use Get-Help to revive more information about the Get-Process command:
Copy code
1
Get-Help -Name Get-Process
Get-Help is a critical cmdlet for understanding the use case of PowerShell cmdlets. It displays
the documentation within the console:
Under SYNTAX are the different combinations that Get-Process can be used, for example, you
could specify the name of a process to return:
Get-Process -Name myprocess
You could also return a process based on the process ID:
Get-Process -Id 32
Most PowerShell cmdlets can make use of multiple combinations of parameters. Any item
surrounded by brackets [ ] indicates that it's optional. So [-Name] is optional and <string[]>
is required, meaning you could search for a process by name using the following formatting:
Get-Process nameofmyprocess
Also, notice the ALIASES section, gps is the alias for Get-Process, meaning you would just use
the alias instead:
gps -Name myprocess
6. Use the following syntax to display more documentation about a cmdlet:
Copy code
1
Get-Help Get-Process -full
It's important to know how to look up cmdlet documentation within the PowerShell console, this
will greatly help you automate tasks with PowerShell.
7. Use a wildcard search * with Get-Process to retrieve all processes:
Copy code
1
Get-Process -Name *
A list of each running process is displayed.
8. Search a process by name:
Copy code
1
Get-Process -Name pwsh
Only the pwsh process is retrieved.
9. Use Get-Help to look up the documentation for the Write-Host command:
Copy code
1
Get-Help write-host
Review the documentation fro the Write-Host cmdlet. You can even use Get-Help write-host
-full to review the full documentation.
10. Use Write-Host to display a message:
Copy code
1
Write-Host -object "Hello World!" -ForegroundColor green
The Write-Host cmdlet is used for displaying output in PowerShell.
11. Click on lab.ps1:
PowerShell scripts end in the .ps1 extension.
12. Copy and paste the following into the lab.ps1 file:
Copy code
1
Write-Host "Hello World!" -ForegroundColor Cyan
The script will produce a "Hello World!" message when executed.
13. In the upper-left corner click File and choose Save:
14. In the terminal, run the script:
Copy code
1
./lab.ps1
You can run the powershell script by specifying the path and name of the script. Simply typing
lab.ps1 would not execute the script.
Summary
In this lab, you learned how to explore PowerShell cmdlets and created a script to display "Hello
World!".