PowerShell First Commands
Let's type some real commands. In an interactive session you type a line after the PS> prompt and press Enter; in a script you write the same lines in a .ps1 file. Everything in this lesson works both ways.
Printing values
Anything you leave on its own line is sent to the output and printed. You rarely need a "print" command at all:
"Good morning"
42
3.5 * 2
'Single quotes work too'
Write-Output "Write-Output does the same thing, explicitly"Good morning
42
7
Single quotes work too
Write-Output does the same thing, explicitly
Write-Output and a bare value are equivalent: both put the value on the output stream, which can be piped to other commands. There is also Write-Host, which writes straight to the screen; you will learn the difference in the Output Streams lesson.
PowerShell as a calculator
The usual arithmetic operators work, and PowerShell understands size suffixes such as KB, MB and GB:
10 + 5 * 2
(10 + 5) * 2
7 / 2
7 % 2
1GB / 1MB
[math]::Round(2 / 3, 3)20
30
3.5
1
1024
0.667
Note that 7 / 2 gives 3.5: unlike C or Java, dividing two integers in PowerShell gives a fractional result when needed. [math]::Round() calls a static method of the .NET Math class.
Navigating folders
PowerShell keeps a current location, like every shell. These cmdlets move around and show where you are (the examples on this site start in /tmp):
Get-Location
Set-Location /
(Get-Location).Path
Set-Location /tmp
New-Item -ItemType Directory -Name demo | Out-Null
Set-Location demo
"Now in: $PWD"
Path
----
/tmp
/
Now in: /tmp/demo
$PWD is an automatic variable that always holds the current location. Out-Null throws away output you don't want to see (here, the description of the new folder).
Aliases: the short names
Typing Set-Location every time would be tiring, so PowerShell has aliases. Many are borrowed from Command Prompt and Unix so that your fingers feel at home:
| Cmdlet | Aliases |
|---|---|
Set-Location |
cd, sl, chdir |
Get-Location |
pwd, gl |
Get-ChildItem |
dir, gci (and ls on Windows) |
Get-Content |
gc, type (and cat on Windows) |
Copy-Item |
copy, cpi (and cp on Windows) |
Remove-Item |
del, ri, rd (and rm on Windows) |
Clear-Host |
cls, clear |
Get-Alias cd, dir, gci | ForEach-Object { "$($_.Name) -> $($_.Definition)" }cd -> Set-Location
dir -> Get-ChildItem
gci -> Get-ChildItem
Warning: On Linux and macOS,
ls,cat,cp,rm,sortand similar names are not aliases. They run the native Unix programs, which return text, not objects. Use the full cmdlet names (orgci,gc) in scripts so they behave the same everywhere.
Comments
# starts a comment that runs to the end of the line. A block comment sits between <# and #>:
# This line is ignored
"visible" # a trailing comment
<#
Everything in here is ignored,
even across several lines.
#>
"also visible"visible
also visible
Useful keys in the console
| Key | What it does |
|---|---|
| Tab | Completes cmdlet names, parameters and paths (press again to cycle) |
| Ctrl+Space | Shows all completions in a menu |
| ↑ / ↓ | Previous / next command in the history |
| Ctrl+R | Search the history |
| Ctrl+C | Stop the running command |
Get-History |
List the commands typed in this session |
Tip: In scripts, always write the full cmdlet name. Aliases are great while typing but make scripts harder to read, and some differ between platforms.
Test yourself
What does 7 / 2 evaluate to in PowerShell?
- 3
- 3.5
- 4
- An error
On Linux, what does ls run inside PowerShell 7?
- Get-ChildItem
- The native /bin/ls program
- Get-Location
- Nothing, it is an error
Which characters start and end a block comment?
- /* and */
- <# and #>
- ## and ##
- <!-- and -->