Powershell: Difference between revisions
Jump to navigation
Jump to search
(→Syntax) |
(→Syntax) |
||
Line 69: | Line 69: | ||
</div> |
</div> |
||
==== 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" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
Line 114: | Line 114: | ||
</div> |
</div> |
||
==== Strings / arrays / dictionaries / ... ==== |
|||
<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"> |
||
Line 140: | Line 140: | ||
</div> |
</div> |
||
==== 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" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
Line 208: | Line 208: | ||
</div> |
</div> |
||
==== 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" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
Line 249: | Line 249: | ||
</div> |
</div> |
||
==== 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" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
Line 277: | Line 277: | ||
</div> |
</div> |
||
==== Exec ==== |
|||
<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" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
Line 294: | Line 294: | ||
& $scriptBlock # 5 |
& $scriptBlock # 5 |
||
$i # 5 |
$i # 5 |
||
# Testing command success with $? or checking for $null |
|||
$res = ... # Some command exec |
|||
if ($? -and $res -ne $null) { |
|||
Write-Host "Command succeeded" |
|||
} |
|||
else { |
|||
Write-Host "Command failed" |
|||
} |
|||
</source> |
</source> |
||
Revision as of 17:05, 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! |
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
Exec
Miscellaneous
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