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 [2023/01/27 23:22] – [Loops/Iteration] mgupton | powershell:qnd:powershell [2024/10/24 14:24] (current) – [Select Objects] mgupton | ||
|---|---|---|---|
| Line 35: | Line 35: | ||
| </ | </ | ||
| - | ===== Data Types ===== | + | ====== Data Types ====== |
| * Use '' | * Use '' | ||
| * [[https:// | * [[https:// | ||
| Line 50: | Line 50: | ||
| </ | </ | ||
| + | <code powershell> | ||
| + | $foo = @( | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ) | ||
| + | </ | ||
| ==== hash table ==== | ==== hash table ==== | ||
| <code powershell> | <code powershell> | ||
| Line 63: | Line 70: | ||
| * If a variable name contains spaces it needs to be wrapped in quotes (e.g. '' | * If a variable name contains spaces it needs to be wrapped in quotes (e.g. '' | ||
| - | ===== Control Structures ===== | + | ====== Control Structures |
| - | ===== Loops/ | + | ====== Loops/ |
| ==== For Loop ==== | ==== For Loop ==== | ||
| - | |||
| <code powershell> | <code powershell> | ||
| $fruit = @(' | $fruit = @(' | ||
| Line 91: | Line 97: | ||
| ==== Hash Table ==== | ==== Hash Table ==== | ||
| - | * To loop over the items in a hash table you have to call '' | + | * 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 107: | Line 131: | ||
| 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 |
| </ | </ | ||