Powershell: Difference between revisions
Jump to navigation
Jump to search
(→Syntax) |
(→Syntax) |
||
Line 56: | Line 56: | ||
Get-Alias -Definition Get-Process |
Get-Alias -Definition Get-Process |
||
gal -def Get-Process |
gal -def Get-Process |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</source> |
</source> |
||
</div> |
</div> |
||
Line 66: | Line 61: | ||
;Primitive types / operators |
;Primitive types / operators |
||
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# Numbers |
# Numbers |
||
3 |
3 |
||
Line 78: | Line 73: | ||
10 % 3 # 1 |
10 % 3 # 1 |
||
[Math]::Pow(2,3) # 8 |
[Math]::Pow(2,3) # 8 |
||
</source> |
|||
<source lang="powershell" style="overflow: hidden"> |
|||
# Boolean |
# Boolean |
||
$True # True |
$True # True |
||
Line 87: | Line 84: | ||
2 -ne $True # True |
2 -ne $True # True |
||
[bool](0) # False |
[bool](0) # False |
||
</source> |
|||
<source lang="powershell" style="overflow: hidden"> |
|||
# Bitwise |
# Bitwise |
||
0 -band 2 # 0 |
0 -band 2 # 0 |
||
-5 -bor 0 # -5 |
-5 -bor 0 # -5 |
||
</source> |
|||
<source lang="powershell" style="overflow: hidden"> |
|||
# Comparison |
# Comparison |
||
1 -eq 1 |
1 -eq 1 |
||
Line 130: | Line 131: | ||
;Control flow |
;Control flow |
||
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# if-then-else |
# if-then-else |
||
if ($someVar -gt 10) { |
if ($someVar -gt 10) { |
||
Line 143: | Line 144: | ||
</source> |
</source> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# foreach |
# foreach |
||
foreach ($animal in ("dog", "cat", "mouse")) { |
foreach ($animal in ("dog", "cat", "mouse")) { |
||
Line 151: | Line 152: | ||
</source> |
</source> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# for |
# for |
||
$letters = ('a','b','c','d','e','f','g','h') |
$letters = ('a','b','c','d','e','f','g','h') |
||
Line 159: | Line 160: | ||
</source> |
</source> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# while |
# while |
||
$x = 0 |
$x = 0 |
||
Line 168: | Line 169: | ||
</source> |
</source> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# switch |
# switch |
||
$val = "20" |
$val = "20" |
||
Line 181: | Line 182: | ||
</source> |
</source> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# try-catch-finally |
# try-catch-finally |
||
# ... |
# ... |
||
Line 190: | Line 191: | ||
;IO |
;IO |
||
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# read / write |
# read / write |
||
Write-Output "Hello World!" # alias: echo, write |
Write-Output "Hello World!" # alias: echo, write |
||
$foo = Read-Host "Enter foo" |
$foo = Read-Host "Enter foo" |
||
$bar = Read-Host ("Enter {0}" -f $foo) |
$bar = Read-Host ("Enter {0}" -f $foo) |
||
</source> |
|||
<source lang="powershell" style="overflow: hidden"> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</source> |
</source> |
||
</div> |
</div> |
||
Line 200: | Line 208: | ||
;Functions |
;Functions |
||
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
<div style="column-width:35em;-webkit-column-width:35em;-moz-column-width:35em;"> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# Functions: keep 'Verb-Noun' convention! |
# Functions: keep 'Verb-Noun' convention! |
||
Revision as of 14:07, 22 February 2022
Links
- Learn powershell in Y minutes — A MUST-READ to learn rapidly.
Reference
Files
Powershell scripts have a .ps1 extension.
Syntax
✐ | See learn Powershell in Y minutes for more! |
# Documentation, information
Update-Help # Update help system - to run as Administrator
help Test-Path # Get help on a command, on an alias...
help Test-Path -full # ... and on all options
help Test-Path -examples # ... see examples. VERY USEFUL. Eg. help save-help examples
Get-Command about_* # DOES NOT WORK
Get-Command -Verb Add
Get-Alias ps # 'Get-Process'
Get-Alias -Definition Get-Process # 'ps'
gal | findstr Get-Process # Unix style
ps | Get-Member
# Frequent aliases (sorted by definition)
# alias -> Get-Alias
# gal -> Get-Alias
# dir -> Get-ChildItem
# ls -> Get-ChildItem
# gci -> Get-ChildItem
# gcm -> Get-Command
# cat -> Get-Content
# type -> Get-Content
# help -> Get-Help
# gm -> Get-Member
# ps -> Get-Process
# man -> help
# ni -> New-Item
# md -> New-Item
# mkdir -> New-Item
# del -> Remove-Item
# erase -> Remove-Item
# rd -> Remove-Item
# ri -> Remove-Item
# rm -> Remove-Item
# rmdir -> Remove-Item
# where -> Where-Object
# echo -> Write-Output
# write -> Write-Output
# Options can be abbreviated
Get-Alias -Definition Get-Process
gal -def Get-Process
- Primitive types / operators
- Strings / arrays / dictionaries / ...
# Strings
$hello = "Hello"
$world = "World"
'Hello, World!' # No interpolation
"Hello, $world!" # 'Hello, World!'
"Hello, World!".Length # Length
"{0}, {1}!" -f $hello, $world # f-string
"$world is $($world.length) char long"
'Hello, World!'[0] # 'H'
'Hello, World!'[0..5] # 'H', 'e', 'l', 'l', 'o'
'Hello, World!'.Substring(0,5 # 'Hello'
'Hello, ' + 'World!' # 'Hello, World!'
'First line`nSecond line' # Escape with backtick
"foo" | gm # Get all methods / properties
# Arrays
# TBC
# Dictionaries
# TBC
- Control flow
- IO
- Functions
Tips
Measure execution time of a command
Measure-Command { dir }
Measure-Command { dir | Out-default} # To get output
Measure-Command { choco list }
Update help on offline computers
From MS devblogs:
<source lang="powershell">
- On online computer
New-Item c:\tmp\help Save-Help -DestinationPath c:\tmp\help -Module * -Force
- On offline computer
- ... transfer files to c:\tmp\help
- ... start powershell in admin (Win-X-A)
Update-Help -SourcePath c:\tmp\help -Module * -Force </source