Getting Started With PowerShell
Getting Started With PowerShell
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.:
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*
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 -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:
It's important to know how to look up cmdlet documentation within the PowerShell console, this
will greatly help you automate tasks with PowerShell.
Copy code
1
Get-Process -Name *
Copy code
1
Get-Process -Name pwsh
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.
Copy code
1
Write-Host -object "Hello World!" -ForegroundColor green
12. Copy and paste the following into the lab.ps1 file:
Copy code
1
Write-Host "Hello World!" -ForegroundColor Cyan
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!".