======Powershell====== * {{ :wiki:comparitech-powershell-cheatsheet.pdf |}} * [[powershell:qnd:Powershell]] (quick-n-dirty) * [[Powershell Configuration]] * [[Running Powershell Scripts]] * [[Powershell File Operations]] * [[Powershell Output]] * [[Powershell Script Arguments]] * [[Powershell Examples]] * [[https://ss64.com/ps]] * [[powershell_git|Powershell Git]] * [[https://jdhitsolutions.com/blog/|The Lonely Administrator]] ====== Interactive Mode ====== ===== History ===== * Use ''h'' or ''history'' command to list history * Use ''# '' to recall the command to the command prompt without executing it * Use ''Invoke-History '' or ''r '' to execute the specified command * Open stored history in text editor. * ''notepad (Get-PSReadlineOption).HistorySavePath'' * Use ''get-history | out-gridview -Passthru | invoke-history'' to get and invoke command from history using a grid view. Alternatively, ''h | ogv -p | r''. Or, just create a function for it, ''function hh {get-history | out-gridview -Passthru | invoke-history}'' * Use ''Ctrl+r'' to interactively search backward through history and ''Ctrl+s'' to search forrward ======Operators====== * [[https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_comparison_operators|Comparison Operators]] ======Variables====== * ''Get-Variable'', to list all variables in the current session ==== Basic Scalar Example ==== $fname = "Spunky" $lname = "Violet" Write-Host "World meet $fname $lname." =====Built-in/Automatic/Special Variables===== * ''$?'', exit code of last operation * ''$ARGS'', array of arguments passed to script. * ''$NULL'', variable that represents //null// value, big surprise. * ''$true'' and ''$false'' represent the boolean values * ''env:'', pseudo drive for environment variables ====== Input/Output ====== Example of using Out-GridView for handy way to get output Get-Alias | Out-GridView Example of selecting a property from objects $files = ls .\* -Exclude @("*.tmp") $files | select -Property FullName # Alternative: this method does not include the property header that is included with the Select-Object method above $files | foreach {$_.FullName} ======Conditionals====== =====If Statement===== if (Test-Path $file -eq $True) { Write-Host "Found a file." } else { Write-Host "No file found." } if ($a -gt 2) { Write-Host "The value $a is greater than 2." } elseif ($a -eq 2) { Write-Host "The value $a is equal to 2." } else { Write-Host "The value $a is less than 2 or was not created or initialized." } ======Miscellanea====== ===Allow all scripts to be ran=== Set-ExecutionPolicy bypass ===One-liner=== * ''-executionpolicy bypass'', overrrides local system policy configuration to allow any script to run. Sometimes you just need to get things done. powershell -executionpolicy bypass -command * [[qnd:Sign Powershell Script]] ====== Related ====== * [[azure:qnd:azure_powershell|Azure Powershell (QnD)]]