powershell:qnd:powershell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
powershell:qnd:powershell [2022/09/28 15:49] – [Select Objects] mguptonpowershell:qnd:powershell [2024/10/24 14:24] (current) – [Select Objects] mgupton
Line 5: Line 5:
   * [[powershell:qnd:File Operations]]   * [[powershell:qnd:File Operations]]
   * [[powershell:qnd:Output]]   * [[powershell:qnd:Output]]
 +  * [[powershell:qnd:Network]]
  
   * [[powershell:qnd:Scripts and Modules]]   * [[powershell:qnd:Scripts and Modules]]
Line 19: Line 20:
   * Get last command error by referencing ''$?'' variable   * Get last command error by referencing ''$?'' variable
   * ''$(<command>)'' is the syntax to do //command substitution//   * ''$(<command>)'' is the syntax to do //command substitution//
 +  * Wrap identifier names in ''{}'' when they contain special characters like spaces and dashes
  
-===== Data Types =====+====== Command Line ====== 
 +  * [[https://stackoverflow.com/questions/71174012/how-do-you-edit-the-command-line-in-an-external-editor|Editing commands in external editor of choice]] 
 + 
 +Example of setting VS Code as the editor used for command line editing 
 +<code powershell> 
 +'@code --new-window --wait %*' > "$HOME\codewait.cmd" 
 + 
 +$env:VISUAL = "$HOME/codewait" 
 + 
 +# Custom key binding 
 +Set-PSReadLineKeyHandler -Chord Alt+e -Function ViEditVisually 
 +</code> 
 + 
 +====== Data Types ======
   * Use ''$var.GetType()'' to get data type of variable   * Use ''$var.GetType()'' to get data type of variable
   * [[https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable|hashtables]]   * [[https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable|hashtables]]
Line 35: Line 50:
 </code> </code>
  
 +<code powershell>
 +$foo = @(
 +       "alpha"
 +       "beta"
 +       "gamma"
 +       )
 +</code>
 ==== hash table ==== ==== hash table ====
 <code powershell> <code powershell>
Line 44: Line 66:
  
 ===== Variables ===== ===== Variables =====
-  * Use ''${var}'' to reference variables that have names that include special characters (e.g. spaces) or are being used to build a string. For building a string this syntax disambiguates the variable name from the rest of the characters in a string. +  * Use ''${<var>}'' to reference variables that have names that include special characters (e.g. spaces) or are being used to build a string. For building a string this syntax disambiguates the variable name from the rest of the characters in a string. 
- +  * ''$(<var>)'' is also needed in certain scenarios, more to come on this 
-===== Loops/Iteration =====+  * If a variable name contains spaces it needs to be wrapped in quotes (e.g. ''$obj."First Name"'' or ''{}''. This might happen with imported CSV data, for example.
  
 +====== Control Structures ======
 +====== Loops/Iteration ======
 +==== For Loop ====
 <code powershell> <code powershell>
 $fruit = @('apple','pear','banana','lemon','lime','mango') $fruit = @('apple','pear','banana','lemon','lime','mango')
Line 53: Line 78:
 For ($i = 0; $i -le $fruit.length; $i++) { For ($i = 0; $i -le $fruit.length; $i++) {
     Write-Host $fruit[$i];         Write-Host $fruit[$i];    
 +}
 +</code>
 +
 +==== Foreach Loop ====
 +
 +<code powershell>
 +foreach ($file in Get-ChildItem) {
 +  # do something with $file
 } }
 </code> </code>
Line 62: Line 95:
  
   * [[https://adamtheautomator.com/powershell-foreach/]]   * [[https://adamtheautomator.com/powershell-foreach/]]
 +
 +==== Hash Table ====
 +  * To loop over the items in a hash table one option is to call ''GetEnumerator()'' on the hash table.
 +
 +<code powershell>
 +$t1 = @{firstname = "Michael"; lastname = "Muse"}
 +
 +foreach ($i in $t1.GetEnumerator()) {
 +    $i.value
 +}
 +</code>
 +
 +=== Alternatively use the keys ===
 +
 +<code powershell>
 +$t1 = @{firstname = "Michael"; lastname = "Muse"}
 +
 +foreach ($i in $t1.keys) {
 +    $t1[$i]
 +}
 +</code>
  
 ====== Select-String ====== ====== Select-String ======
Line 77: 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 -first 1
 </code> </code>
  
  • powershell/qnd/powershell.1664380143.txt.gz
  • Last modified: 2022/09/28 15:49
  • by mgupton