If you are new to PowerShell, this document will walk you through a few examples to give you some basic ideas of PowerShell. We recommend that you open a PowerShell console/session and type along with the instructions in this document to get most out of this exercise.
First you need to launch a PowerShell session by following the Installing PowerShell Guide.
In this section, you will learn how to
- create a file, delete a file and change file directory
- discover what version of PowerShell you are currently using
- exit a PowerShell session
- get help if you needed
- find syntax of PowerShell cmdlets
- and more
As mentioned above, PowerShell commands are designed to have Verb-Noun structure, for instance Get-Process, Set-Location, Clear-Host, etc.
Let’s exercise some of the basic PowerShell commands, also known as cmdlets.
Please note that we will use the PowerShell prompt sign PS /> as it appears on Linux in the following examples.
It is shown as PS C:\> on Windows.
-
Get-Process: Gets the processes that are running on the local computer or a remote computer.By default, you will get data back similar to the following:
PS /> Get-Process Handles NPM(K) PM(K) WS(K) CPU(s) Id ProcessName ------- ------ ----- ----- ------ -- ----------- - - - 1 0.012 12 bash - - - 21 20.220 449 powershell - - - 11 61.630 8620 code - - - 74 403.150 1209 firefox …
Only interested in the instance of Firefox process that is running on your computer?
Try this:
PS /> Get-Process -Name firefox Handles NPM(K) PM(K) WS(K) CPU(s) Id ProcessName ------- ------ ----- ----- ------ -- ----------- - - - 74 403.150 1209 firefox
Want to get back more than one process? Then just specify process names and separate them with commas.
PS /> Get-Process -Name firefox, powershell Handles NPM(K) PM(K) WS(K) CPU(s) Id ProcessName ------- ------ ----- ----- ------ -- ----------- - - - 74 403.150 1209 firefox - - - 21 20.220 449 powershell
-
Clear-Host: Clears the display in the host program.PS /> Get-Process PS /> Clear-Host
Type too much just for clearing the screen?
Here is how the alias can help.
-
Get-Alias: Gets the aliases for the current session.Get-Alias CommandType Name ----------- ---- … Alias cd -> Set-Location Alias cls -> Clear-Host Alias clear -> Clear-Host Alias copy -> Copy-Item Alias dir -> Get-ChildItem Alias gc -> Get-Content Alias gmo -> Get-Module Alias ri -> Remove-Item Alias type -> Get-Content …
As you can see
clsorclearis an alias ofClear-Host.Now try it:
PS /> Get-Process PS /> cls
-
cd -> Set-Location: Sets the current working location to a specified location.PS /> Set-Location /home PS /home>
-
dir -> Get-ChildItem: Gets the items and child items in one or more specified locations.# Get all files under the current directory: PS /> Get-ChildItem # Get all files under the current directory as well as its subdirectories: PS /> cd $home PS /home/jen> dir -Recurse # List all files with "txt" file extension. PS /> cd $home PS /home/jen> dir –Path *.txt -Recurse
-
New-Item: Creates a new item.# An empty file is created if you type the following: PS /home/jen> New-Item -Path ./test.txt Directory: /home/jen Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 7/7/2016 7:17 PM 0 test.txt
You can use the
-Valueparameter to add some data to your file.For example, the following command adds the phrase
Hello world!as a file content to thetest.txt.Because the test.txt file exists already, we use
-Forceparameter to replace the existing content.PS /home/jen> New-Item -Path ./test.txt -Value "Hello world!" -Force Directory: /home/jen Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 7/7/2016 7:19 PM 24 test.txt
There are other ways to add some data to a file.
For example, you can use
Set-Contentto set the file contents:PS /home/jen>Set-Content -Path ./test.txt -Value "Hello world again!"
Or simply use
>as below:# create an empty file "" > test.txt # set "Hello world!" as content of test.txt file "Hello world!!!" > test.txt
The pound sign
#above is used for comments in PowerShell. -
type -> Get-Content: Gets the content of the item at the specified location.PS /home/jen> Get-Content -Path ./test.txt PS /home/jen> type -Path ./test.txt Hello world again!
-
del -> Remove-Item: Deletes the specified items.This cmdlet will delete the file
/home/jen/test.txt:PS /home/jen> Remove-Item ./test.txt
-
$PSVersionTable: Displays the version of PowerShell you are currently using.Type
$PSVersionTablein your PowerShell session, you will see something like below. "PSVersion" indicates the PowerShell version that you are using.Name Value ---- ----- PSVersion 6.0.0-alpha PSEdition Core PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 3.0.0.0 GitCommitId v6.0.0-alpha.12 CLRVersion WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
-
Exit: To exit the PowerShell session, typeexit.exit
The most important command in PowerShell is possibly the Get-Help, which allows you to quickly learn PowerShell without having to search around the internet.
The Get-Help cmdlet also shows you how PowerShell commands work with examples.
It shows the syntax and other technical information of the Get-Process cmdlet.
PS /> Get-Help -Name Get-ProcessIt displays the examples how to use the Get-Process cmdlet.
PS />Get-Help -Name Get-Process -ExamplesIf you use -Full parameter, for example, Get-Help -Name Get-Process -Full, it will display more technical information.
You want to discover what PowerShell cmdlets available on your system? Just run Get-Command as below:
PS /> Get-CommandIf you want to know whether a particular cmdlet exists on your system, you can do something like below:
PS /> Get-Command Get-ProcessIf you want to know the syntax of Get-Process cmdlet, type:
PS /> Get-Command Get-Process -SyntaxIf you want to know how to use the Get-Process, type:
PS /> Get-Help Get-Process -ExampleSometimes when you run Get-ChildItem or "dir", you want to get a list of files and folders in a descending order. To achieve that, type:
PS /home/jen> dir | Sort-Object -DescendingSay you want to get the largest file in a directory
PS /home/jen> dir | Sort-Object -Property Length -Descending | Select-Object -First 1
Directory: /home/jen
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/16/2016 1:15 PM 32972 test.log
You can use Visual Studio Code or your favorite editor to create a PowerShell script and save it with a .ps1 file extension.
For more details, see Create and Run PowerShell Script Guide
- Video: Get Started with PowerShell from Channel9
- eBooks from PowerShell.org
- eBooks List by Martin Schvartzman
- Tutorial from MVP
- Script Guy blog: The best way to Learn PowerShell
- Understanding PowerShell Module
- How and When to Create PowerShell Module by Adam Bertram
- Video: PowerShell Remoting in Depth from Channel9
- PowerShell Basics: Remote Management from ITPro
- Running Remote Commands from PowerShell Web Docs
- Samples for Writing a PowerShell Script Module
- Writing a PowerShell module in C#
- Examples of Cmdlets Code
- Windows PowerShell in Action by Bruce Payette
- Windows PowerShell Cookbook by Lee Holmes