2018-08-31 14:32:54 -07:00
# PowerShell Beginner’ s Guide
2016-07-12 13:21:15 -07:00
2016-07-21 19:09:45 -07:00
If you are new to PowerShell, this document will walk you through a few examples to give you some basic ideas of PowerShell.
2016-07-17 11:35:04 +02:00
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.
2016-07-12 13:21:15 -07:00
2018-08-31 14:32:54 -07:00
## Launch PowerShell Console/Session
2016-07-12 13:21:15 -07:00
2016-08-01 11:21:55 -07:00
First you need to launch a PowerShell session by following the [Installing PowerShell Guide ](./README.md#installing-powershell ).
2016-07-12 13:21:15 -07:00
2018-08-31 14:32:54 -07:00
## Getting Familiar with PowerShell Commands
2016-07-12 13:21:15 -07:00
2016-07-14 17:50:56 -07:00
In this section, you will learn how to
2018-08-31 14:32:54 -07:00
2016-07-14 17:50:56 -07:00
- create a file, delete a file and change file directory
- discover what version of PowerShell you are currently using
2016-07-14 18:06:50 -07:00
- exit a PowerShell session
2016-07-21 19:09:45 -07:00
- get help if you needed
- find syntax of PowerShell cmdlets
2016-07-14 17:50:56 -07:00
- and more
2016-07-12 13:21:15 -07:00
2018-09-24 18:57:53 +02:00
As mentioned above, PowerShell commands are designed to have Verb-Noun structure, for instance `Get-Process` , `Set-Location` , `Clear-Host` , etc.
2016-07-17 11:35:04 +02:00
Let’ s exercise some of the basic PowerShell commands, also known as **cmdlets** .
2016-07-12 13:21:15 -07:00
2016-07-14 17:50:56 -07:00
Please note that we will use the PowerShell prompt sign **PS />** as it appears on Linux in the following examples.
2018-09-24 18:57:53 +02:00
It is shown as `PS C:\>` on Windows.
2016-07-13 19:18:29 -07:00
2018-09-24 18:57:53 +02:00
1. `Get-Process` : Gets the processes that are running on the local computer or a remote computer.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
By default, you will get data back similar to the following:
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
```powershell
PS /> Get-Process
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
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
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
…
` ``
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
Only interested in the instance of Firefox process that is running on your computer?
2018-09-24 18:57:53 +02:00
2020-05-28 09:29:56 +01:00
Try this:
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /> Get-Process -Name firefox
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Handles NPM(K) PM(K) WS(K) CPU(s) Id ProcessName
------- ------ ----- ----- ------ -- -----------
- - - 74 403.150 1209 firefox
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
Want to get back more than one process?
Then just specify process names and separate them with commas.
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
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
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``
2016-07-12 13:21:15 -07:00
2019-03-05 15:45:46 -05:00
1. ` Clear-Host`: Clears the display in the host program.
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /> Get-Process
PS /> Clear-Host
` ``
2019-03-05 15:45:46 -05:00
2020-05-28 09:29:56 +01:00
Type too much just for clearing the screen?
2018-09-24 18:57:53 +02:00
2020-05-28 09:29:56 +01:00
Here is how the alias can help.
2016-07-12 13:21:15 -07:00
2019-03-05 15:45:46 -05:00
1. ` Get-Alias`: Gets the aliases for the current session.
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
Get-Alias
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
CommandType Name
----------- ----
…
2016-07-13 19:18:29 -07:00
2020-05-28 09:29:56 +01:00
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
…
` ``
2018-09-24 18:57:53 +02:00
2020-05-28 09:29:56 +01:00
As you can see ` cls` or ` clear` is an alias of ` Clear-Host`.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Now try it:
2019-03-05 15:45:46 -05:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /> Get-Process
PS /> cls
` ``
2018-08-31 14:32:54 -07:00
2019-03-05 15:45:46 -05:00
1. ` cd -> Set-Location`: Sets the current working location to a specified location.
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /> Set-Location /home
PS /home>
` ``
2018-08-31 14:32:54 -07:00
2019-03-05 15:45:46 -05:00
1. ` dir -> Get-ChildItem`: Gets the items and child items in one or more specified locations.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
# Get all files under the current directory:
PS /> Get-ChildItem
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
# Get all files under the current directory as well as its subdirectories:
PS /> cd $home
PS /home/jen> dir -Recurse
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
# List all files with "txt" file extension.
PS /> cd $home
PS /home/jen> dir – Path *.txt -Recurse
` ``
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
1. ` New-Item`: Creates a new item.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
# An empty file is created if you type the following:
PS /home/jen> New-Item -Path ./test.txt
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Directory: /home/jen
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 7/7/2016 7:17 PM 0 test.txt
` ``
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
You can use the ` -Value` parameter to add some data to your file.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
For example, the following command adds the phrase ` Hello world!` as a file content to the ` test.txt`.
2018-09-24 18:57:53 +02:00
2020-05-28 09:29:56 +01:00
Because the test.txt file exists already, we use ` -Force` parameter to replace the existing content.
2018-09-24 18:57:53 +02:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /home/jen> New-Item -Path ./test.txt -Value "Hello world!" -Force
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Directory: /home/jen
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 7/7/2016 7:19 PM 24 test.txt
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
There are other ways to add some data to a file.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
For example, you can use ` Set-Content` to set the file contents:
2018-09-24 18:57:53 +02:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /home/jen>Set-Content -Path ./test.txt -Value "Hello world again!"
` ``
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
Or simply use ` >` as below:
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
# create an empty file
"" > test.txt
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
# set "Hello world!" as content of test.txt file
"Hello world!!!" > test.txt
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
The pound sign ` #` above is used for comments in PowerShell.
2016-07-12 13:21:15 -07:00
2019-03-05 15:45:46 -05:00
1. ` type -> Get-Content`: Gets the content of the item at the specified location.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /home/jen> Get-Content -Path ./test.txt
PS /home/jen> type -Path ./test.txt
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
Hello world again!
` ``
2018-08-31 14:32:54 -07:00
2019-03-05 15:45:46 -05:00
1. ` del -> Remove-Item`: Deletes the specified items.
2016-07-12 13:21:15 -07:00
2020-05-28 09:29:56 +01:00
This cmdlet will delete the file ` /home/jen/test.txt`:
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
PS /home/jen> Remove-Item ./test.txt
` ``
2016-07-14 17:50:56 -07:00
2019-03-05 15:45:46 -05:00
1. ` $PSVersionTable`: Displays the version of PowerShell you are currently using.
2016-07-14 17:50:56 -07:00
2020-05-28 09:29:56 +01:00
Type ` $PSVersionTable` in your PowerShell session, you will see something like below.
"PSVersion" indicates the PowerShell version that you are using.
2016-07-14 17:50:56 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
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
2016-07-14 17:50:56 -07:00
2020-05-28 09:29:56 +01:00
` ``
2016-07-14 17:50:56 -07:00
2019-03-05 15:45:46 -05:00
1. ` Exit`: To exit the PowerShell session, type ` exit`.
2018-08-31 14:32:54 -07:00
2020-05-28 09:29:56 +01:00
` ``powershell
exit
` ``
2016-07-12 13:21:15 -07:00
2018-08-31 14:32:54 -07:00
## Need Help?
2018-09-24 18:57:53 +02:00
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.
2016-07-12 13:21:15 -07:00
2018-09-24 18:57:53 +02:00
The ` Get-Help` cmdlet also shows you how PowerShell commands work with examples.
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
It shows the syntax and other technical information of the ` Get-Process` cmdlet.
` ``powershell
2016-07-17 12:17:17 +02:00
PS /> Get-Help -Name Get-Process
` ``
2016-07-12 13:21:15 -07:00
2018-09-24 18:57:53 +02:00
It displays the examples how to use the ` Get-Process` cmdlet.
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-08-01 11:21:55 -07:00
PS />Get-Help -Name Get-Process -Examples
2016-07-17 12:17:17 +02:00
` ``
If you use **-Full** parameter, for example, ` Get-Help -Name Get-Process -Full`, it will display more technical information.
2016-07-12 13:21:15 -07:00
2018-08-31 14:32:54 -07:00
## Discover Commands Available on Your System
2016-07-12 13:21:15 -07:00
2018-09-24 18:57:53 +02:00
You want to discover what PowerShell cmdlets available on your system? Just run ` Get-Command` as below:
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-07-17 12:17:17 +02:00
PS /> Get-Command
` ``
2018-08-31 14:32:54 -07:00
2016-07-12 13:21:15 -07:00
If you want to know whether a particular cmdlet exists on your system, you can do something like below:
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-07-17 12:17:17 +02:00
PS /> Get-Command Get-Process
` ``
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
If you want to know the syntax of ` Get-Process` cmdlet, type:
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-07-17 12:17:17 +02:00
PS /> Get-Command Get-Process -Syntax
` ``
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
If you want to know how to use the ` Get-Process`, type:
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-07-17 12:17:17 +02:00
PS /> Get-Help Get-Process -Example
` ``
2016-07-12 13:21:15 -07:00
2018-09-24 18:57:53 +02:00
## PowerShell Pipeline ` |`
2018-08-31 14:32:54 -07:00
2016-07-21 19:09:45 -07:00
Sometimes when you run Get-ChildItem or "dir", you want to get a list of files and folders in a descending order.
2016-07-17 11:35:04 +02:00
To achieve that, type:
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-08-17 21:04:11 -07:00
PS /home/jen> dir | Sort-Object -Descending
2016-07-12 13:21:15 -07:00
` ``
2018-08-31 14:32:54 -07:00
2016-07-12 13:21:15 -07:00
Say you want to get the largest file in a directory
2018-08-31 14:32:54 -07:00
2018-09-24 18:57:53 +02:00
` ``powershell
2016-07-13 19:18:29 -07:00
PS /home/jen> dir | Sort-Object -Property Length -Descending | Select-Object -First 1
2016-07-12 13:21:15 -07:00
2016-07-13 19:18:29 -07:00
Directory: /home/jen
2016-07-12 13:21:15 -07:00
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/16/2016 1:15 PM 32972 test.log
` ``
2018-08-31 14:32:54 -07:00
## How to Create and Run PowerShell scripts
2016-08-12 13:45:24 -07:00
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][create-run-script]
2016-07-12 13:21:15 -07:00
2018-08-31 14:32:54 -07:00
## Recommended Training and Reading
2016-07-12 13:21:15 -07:00
2017-04-11 09:44:43 -07:00
- Video: [Get Started with PowerShell][remoting] from Channel9
2019-01-15 16:20:45 -08:00
- [eBooks from PowerShell.org ](https://leanpub.com/u/devopscollective )
2016-07-21 19:09:45 -07:00
- [eBooks List][ebook-list] by Martin Schvartzman
2016-07-12 13:21:15 -07:00
- [Tutorial from MVP][tutorial]
2017-04-11 09:44:43 -07:00
- Script Guy blog: [The best way to Learn PowerShell][to-learn]
- [Understanding PowerShell Module][ps-module]
- [How and When to Create PowerShell Module][create-ps-module] by Adam Bertram
2016-07-12 13:21:15 -07:00
- Video: [PowerShell Remoting in Depth][in-depth] from Channel9
2017-04-11 09:44:43 -07:00
- [PowerShell Basics: Remote Management][remote-mgmt] from ITPro
2016-07-12 13:21:15 -07:00
- [Running Remote Commands][remote-commands] from PowerShell Web Docs
- [Samples for Writing a PowerShell Script Module][examples-ps-module]
- [Writing a PowerShell module in C#][writing-ps-module]
- [Examples of Cmdlets Code][sample-code]
2018-08-31 14:32:54 -07:00
## Commercial Resources
2016-07-12 13:21:15 -07:00
2016-07-21 19:09:45 -07:00
- [Windows PowerShell in Action][in-action] by Bruce Payette
- [Windows PowerShell Cookbook][cookbook] by Lee Holmes
2019-01-15 16:20:45 -08:00
[in-action]: https://www.amazon.com/Windows-PowerShell-Action-Bruce-Payette/dp/1633430294
2016-07-12 13:21:15 -07:00
[cookbook]: http://shop.oreilly.com/product/9780596801519.do
2019-01-15 16:20:45 -08:00
[ebook-list]: https://martin77s.wordpress.com/2014/05/26/free-powershell-ebooks/
2019-01-24 20:50:11 +00:00
[tutorial]: https://www.computerperformance.co.uk/powershell/index.htm
2016-07-12 13:21:15 -07:00
[to-learn]:https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/04/weekend-scripter-the-best-ways-to-learn-powershell/
2019-10-12 21:28:40 +05:00
[ps-module]:https://docs.microsoft.com/powershell/scripting/developer/module/understanding-a-windows-powershell-module
2019-03-05 15:45:46 -05:00
[create-ps-module]:https://www.business.com/articles/powershell-modules/
2016-07-12 13:21:15 -07:00
[remoting]:https://channel9.msdn.com/Series/GetStartedPowerShell3/06
[in-depth]: https://channel9.msdn.com/events/MMS/2012/SV-B406
2019-03-11 10:41:06 -07:00
[remote-mgmt]:https://www.itprotoday.com/powershell/powershell-basics-remote-management
2019-11-15 12:02:43 -08:00
[remote-commands]:https://docs.microsoft.com/powershell/scripting/learn/remoting/running-remote-commands
2019-10-12 21:28:40 +05:00
[examples-ps-module]:https://docs.microsoft.com/powershell/scripting/developer/module/how-to-write-a-powershell-script-module
2019-01-24 20:50:11 +00:00
[writing-ps-module]:https://www.powershellmagazine.com/2014/03/18/writing-a-powershell-module-in-c-part-1-the-basics/
2019-10-12 21:28:40 +05:00
[sample-code]:https://docs.microsoft.com/powershell/scripting/developer/cmdlet/examples-of-cmdlet-code
2016-08-12 13:45:24 -07:00
[create-run-script]:./create-powershell-scripts.md