Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
powershell:qnd:powershell [2022/09/28 13:33] – [Loops] mgupton | powershell:qnd:powershell [2024/10/24 14:24] (current) – [Select Objects] mgupton | ||
---|---|---|---|
Line 5: | Line 5: | ||
* [[powershell: | * [[powershell: | ||
* [[powershell: | * [[powershell: | ||
+ | * [[powershell: | ||
* [[powershell: | * [[powershell: | ||
Line 19: | Line 20: | ||
* Get last command error by referencing '' | * Get last command error by referencing '' | ||
* '' | * '' | ||
+ | * Wrap identifier names in '' | ||
- | ===== Data Types ===== | + | ====== Command Line ====== |
+ | * [[https:// | ||
+ | |||
+ | Example of setting VS Code as the editor used for command line editing | ||
+ | <code powershell> | ||
+ | '@code --new-window --wait %*' > " | ||
+ | |||
+ | $env:VISUAL = " | ||
+ | |||
+ | # Custom key binding | ||
+ | Set-PSReadLineKeyHandler -Chord Alt+e -Function ViEditVisually | ||
+ | </ | ||
+ | |||
+ | ====== Data Types ====== | ||
* Use '' | * Use '' | ||
* [[https:// | * [[https:// | ||
Line 35: | Line 50: | ||
</ | </ | ||
+ | <code powershell> | ||
+ | $foo = @( | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ) | ||
+ | </ | ||
==== hash table ==== | ==== hash table ==== | ||
<code powershell> | <code powershell> | ||
Line 44: | Line 66: | ||
===== Variables ===== | ===== Variables ===== | ||
- | * Use '' | + | * Use '' |
- | + | * '' | |
- | ===== Loops ===== | + | * If a variable name contains spaces it needs to be wrapped in quotes (e.g. '' |
+ | ====== Control Structures ====== | ||
+ | ====== Loops/ | ||
+ | ==== For Loop ==== | ||
<code powershell> | <code powershell> | ||
$fruit = @(' | $fruit = @(' | ||
Line 53: | Line 78: | ||
For ($i = 0; $i -le $fruit.length; | For ($i = 0; $i -le $fruit.length; | ||
Write-Host $fruit[$i]; | Write-Host $fruit[$i]; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Foreach Loop ==== | ||
+ | |||
+ | <code powershell> | ||
+ | foreach ($file in Get-ChildItem) { | ||
+ | # do something with $file | ||
} | } | ||
</ | </ | ||
Line 62: | Line 95: | ||
* [[https:// | * [[https:// | ||
+ | |||
+ | ==== Hash Table ==== | ||
+ | * To loop over the items in a hash table one option is to call '' | ||
+ | |||
+ | <code powershell> | ||
+ | $t1 = @{firstname = " | ||
+ | |||
+ | foreach ($i in $t1.GetEnumerator()) { | ||
+ | $i.value | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Alternatively use the keys === | ||
+ | |||
+ | <code powershell> | ||
+ | $t1 = @{firstname = " | ||
+ | |||
+ | foreach ($i in $t1.keys) { | ||
+ | $t1[$i] | ||
+ | } | ||
+ | </ | ||
====== Select-String ====== | ====== Select-String ====== | ||
Line 73: | Line 127: | ||
====== Select Objects ====== | ====== Select Objects ====== | ||
+ | * '' | ||
Example of getting just the first item of the listed output | Example of getting just the first item of the listed output | ||
<code powershell> | <code powershell> | ||
- | ls | select first 1 | + | ls | select |
</ | </ | ||
Line 89: | Line 144: | ||
Get-NetIPAddress | Select-Object InterfaceAlias, | Get-NetIPAddress | Select-Object InterfaceAlias, | ||
</ | </ | ||
+ | |||
+ | ===== Expand Property ===== | ||
+ | |||
====== Get Objects - Where-Object ====== | ====== Get Objects - Where-Object ====== |