Powershell tips
Here I will collect some interesting things about powershell, just to keep it in one place. Quick and quite comprehensive info can be found at ss64-powershell-syntax.
- it is good to update help, so one can use Get-Help as a first hand information
PS C:\> Update-Help
PS C:\> Get-Help Set-ExecutionPolicy- in case to run script from shared location, change of execution policy is needed - for current command or permanently(see all possiblities using Get-Help)
PS C:\> Set-ExecutionPolicy Bypass -Scope Process -Force; \\share\somescript.ps1
PS C:\> Set-ExecutionPolicy -ExecutionPolicy bypass -Scope CurrentUser- except commont assignment
PS C:\> $a = 5
PS C:\> $a
5
PS C:\> $a +=2
PS C:\> $a
7- in powershell exists aswell multiassignmet - see about-assigment-operators on the end of page
PS C:\> $a, $b = 1, 2
PS C:\> $a, $b
1
2
PS C:\>- I has found more about it due to zip-function - to be assigned variable $a1 can be array and assignment assign first member of array to $x and rest of array to $a1. It quit ressamble fsharp head::tail match expression. Only don’t know if such a construction is somewhat effective or just rest of array is copied. Btw. arrays in powershell are immutable.
function Zip($a1, $a2) {
while ($a1) {
$x, $a1 = $a1
$y, $a2 = $a2
[tuple]::Create($x, $y)
}
}- powershell functions are first class citiziens - they can be assigned to variable and call by prepending call operator & in front of it - see ss64-powershell-calloperator
PS C:\> $a = { param($x,$y) Write-Host $x $y}
PS C:\> &$a 1 2
1 2
PS C:\> & { param($x,$y) Write-Host $x $y}
PS C:\> & { param($x,$y) Write-Host $x $y} 1 2
1 2- you can get curried function too, important is to use GetNewClosure(), I have found more info about functional programming here lambda-functions, functional-programming-in-powershell_1, functional-programming-in-powershell_2
PS C:\> $b = {param ($x) &$a 5 $x}.GetNewClosure()
PS C:\> &$b 7
5 7