Difference between revisions of "PowerShell/Core7"

From Ever changing code
Jump to navigation Jump to search
Line 20: Line 20:
Any commands included in the profile file will be executed during terminal/shell initialization.
Any commands included in the profile file will be executed during terminal/shell initialization.
{{Note|This is not working while switching the shell and is executed on the new terminal initiation}}
{{Note|This is not working while switching the shell and is executed on the new terminal initiation}}
<syntaxhighlightjs lang="powershell">
<syntaxhighlightjsjs lang="powershell">
> $profile
> $profile
/home/piotr/.config/powershell/Microsoft.VSCode_profile.ps1
/home/piotr/.config/powershell/Microsoft.VSCode_profile.ps1
</syntaxhighlightjs>
</syntaxhighlightjsjs>


= Pipeline =
= Pipeline =
Everything in PS is '''an object''', although we use '|' piping this is not the same concept as STDIN and STDOUT in Linux.
Everything in PS is '''an object''', although we use '|' piping this is not the same concept as STDIN and STDOUT in Linux.
<syntaxhighlight lang="powershell">
<syntaxhighlightjs lang="powershell">
PS> Get-Process -Name whoopsie | Get-Member -Name CPU,Company
PS> Get-Process -Name whoopsie | Get-Member -Name CPU,Company
PS> Get-Process -Name whoopsie | Select-Object -Property name,CPU  
PS> Get-Process -Name whoopsie | Select-Object -Property name,CPU  
Line 46: Line 46:
Web Content      5085.47
Web Content      5085.47
Xorg              3593.26
Xorg              3593.26
</syntaxhighlight>
</syntaxhighlightjs>


= Variables =
= Variables =
These are objects.  
These are objects.  
<syntaxhighlight lang="powershell">
<syntaxhighlightjs lang="powershell">
S /home/piotr> Get-Help *variable*
S /home/piotr> Get-Help *variable*
Name                              Category  Module                    Synopsis                                                                                                                                                                                                                                       
Name                              Category  Module                    Synopsis                                                                                                                                                                                                                                       
Line 59: Line 59:
Remove-Variable                  Cmdlet    Microsoft.PowerShell.Uti… …
Remove-Variable                  Cmdlet    Microsoft.PowerShell.Uti… …
Set-Variable                      Cmdlet    Microsoft.PowerShell.Uti… …
Set-Variable                      Cmdlet    Microsoft.PowerShell.Uti… …
</syntaxhighlight>
</syntaxhighlightjs>


= if =
= if =
<syntaxhighlight lang="powershell">
<syntaxhighlightjs lang="powershell">
if ($var -gt 5) {
if ($var -gt 5) {
   write-host "true"
   write-host "true"
Line 68: Line 68:
   write host "false"
   write host "false"
}
}
</syntaxhighlight>
</syntaxhighlightjs>


= do/while =
= do/while =
<syntaxhighlight lang="powershell">
<syntaxhighlightjs lang="powershell">
Do {
Do {
     $number = $number + 1  
     $number = $number + 1  
     write-host "current nuber is: $number"
     write-host "current nuber is: $number"
} while ($number -lt 10)
} while ($number -lt 10)
</syntaxhighlight>
</syntaxhighlightjs>


= for each =
= for each =
<syntaxhighlight lang="powershell">
<syntaxhighlightjs lang="powershell">
$names = "Bob","Jamse","Tom"
$names = "Bob","Jamse","Tom"
$collection = $names
$collection = $names
Line 88: Line 88:
}
}
Write-Host "$count"
Write-Host "$count"
</syntaxhighlight>
</syntaxhighlightjs>

Revision as of 19:07, 2 July 2020

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 uni

Environment profiles

Any commands included in the profile file will be executed during terminal/shell initialization. Note: This is not working while switching the shell and is executed on the new terminal initiation <syntaxhighlightjsjs lang="powershell"> > $profile /home/piotr/.config/powershell/Microsoft.VSCode_profile.ps1 </syntaxhighlightjsjs>

Pipeline

Everything in PS is an object, although we use '|' piping this is not the same concept as STDIN and STDOUT in Linux. <syntaxhighlightjs lang="powershell"> 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

  1. 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 </syntaxhighlightjs>

Variables

These are objects. <syntaxhighlightjs lang="powershell"> 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… … </syntaxhighlightjs>

if

<syntaxhighlightjs lang="powershell"> if ($var -gt 5) {

 write-host "true"

} elseif {

 write host "false"

} </syntaxhighlightjs>

do/while

<syntaxhighlightjs lang="powershell"> Do {

   $number = $number + 1 
   write-host "current nuber is: $number"

} while ($number -lt 10) </syntaxhighlightjs>

for each

<syntaxhighlightjs lang="powershell"> $names = "Bob","Jamse","Tom" $collection = $names $count = 0 foreach ($item in $collection) {

   $count += 1
   Write-Host "$item"

} Write-Host "$count" </syntaxhighlightjs>