Powershell: Difference between revisions
Jump to navigation
Jump to search
(→Syntax) |
|||
Line 8: | Line 8: | ||
=== Syntax === |
=== Syntax === |
||
{{Note|See learn Powershell in Y minutes for more!}} |
{{Note|See [https://learnxinyminutes.com/docs/powershell/ learn Powershell in Y minutes] for more!}} |
||
<div style="column-width:40em;-webkit-column-width:40em;-moz-column-width:40em;"> |
<div style="column-width:40em;-webkit-column-width:40em;-moz-column-width:40em;"> |
||
<source lang="powershell"> |
<source lang="powershell" style="overflow: hidden"> |
||
# Documentation, information |
# Documentation, information |
||
Update-Help # Update help system - to run as Administrator |
Update-Help # Update help system - to run as Administrator |
||
Line 27: | Line 27: | ||
ps | Get-Member |
ps | Get-Member |
||
⚫ | |||
⚫ | |||
⚫ | |||
# Single line comments start with a number symbol. |
|||
<# |
|||
Multi-line comments |
|||
like so |
|||
#> |
|||
</source> |
|||
<source lang="powershell" style="overflow: hidden"> |
|||
# Frequent aliases (sorted by definition) |
# Frequent aliases (sorted by definition) |
||
# alias -> Get-Alias |
# alias -> Get-Alias |
||
Line 53: | Line 66: | ||
# write -> Write-Output |
# write -> Write-Output |
||
⚫ | |||
⚫ | |||
⚫ | |||
</source> |
</source> |
||
</div> |
</div> |
||
Line 184: | Line 194: | ||
<source lang="powershell" style="overflow: hidden"> |
<source lang="powershell" style="overflow: hidden"> |
||
# try-catch-finally |
# try-catch-finally |
||
try { |
|||
# ... |
|||
throw "This is an error" # Use "throw" to raise an error |
|||
} |
|||
catch { |
|||
Write-Output $Error.ExceptionMessage |
|||
} |
|||
finally { |
|||
Write-Output "We can clean up resources here" |
|||
} |
|||
</source> |
</source> |
||
Revision as of 16:22, 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