PowerShell/Core7

From Ever changing code
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>

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… …