PowerShell/Core7
Jump to navigation
Jump to search
Installing PowerShell on Linux
Note: Ubuntu 20.04 is an LTS release. PowerShell does not currently support this version. Support for this version is being considered for the PowerShell 7.1 release. PowerShell can only support the distributions that are supported by .NET. See the Core release notes for a list of supported distributions.
snap install powershell --classic powershell 7.0.2 from Microsoft PowerShell✓ installed $ powershell # run or use 'pwsh' to start it up PowerShell 7.0.2 Copyright (c) Microsoft Corporation. All rights reserved. https://aka.ms/powershell Type 'help' to get help. PS /home/piotr>
Concepts
- module - set of related Windows PowerShell functionalities grouped together as convenient unit
Pipeline
Everything in PS is an object, although we use '|' piping this is not the same concept as STDIN and STDOUT in Linux.
PS> Get-Process -Name whoopsie | Get-Member -Name CPU,Company PS> Get-Process -Name whoopsie | Select-Object -Property name,CPU Name CPU ---- --- whoopsie 0.24 # When assigning to a variable we assign an object not just a string $variable = Get-Process S> $variable | Select-Object -Property name,CPU | Sort-Object -Descending CPU | Select-Object -First 5 Name CPU ---- --- gnome-shell 11796.69 firefox 11012.4599999 Web Content 7573.22 Web Content 5085.47 Xorg 3593.26
Variables
These are objects.
S /home/piotr> Get-Help *variable* Name Category Module Synopsis ---- -------- ------ -------- Clear-Variable Cmdlet Microsoft.PowerShell.Uti… … Get-Variable Cmdlet Microsoft.PowerShell.Uti… … New-Variable Cmdlet Microsoft.PowerShell.Uti… … Remove-Variable Cmdlet Microsoft.PowerShell.Uti… … Set-Variable Cmdlet Microsoft.PowerShell.Uti… …
if
if ($var -gt 5) { write-host "true" } elseif { write host "false" }
do/while
Do { $number = $number + 1 write-host "current nuber is: $number" } while ($number -lt 10)
for each
$names = "Bob","Jamse","Tom" $collection = $names $count = 0 foreach ($item in $collection) { $count += 1 Write-Host "$item" } Write-Host "$count"