Using PowerShell Core is very simple. This recipe will show you the very first steps and help you to run PowerShell Core after the installation.
Running PowerShell Core
Getting ready
In order to follow this recipe, you should have completed the installation of PowerShell Core for your operating system.
How to do it...
Let's perform the following steps:
- On Windows, run pwsh.exe. On Linux or macOS, run pwsh.
- Type your first cmdlet, Get-Process, to retrieve a list of running processes on the system and hit Enter to confirm.
- Compare the output of the cmdlet with the output of tasklist (in Windows) and ps (in Linux):
- Type Get-Date and hit Enter to confirm.
- Compare the output of this cmdlet with the output of date /t (in Windows) and date (in Linux):
- Execute the line: Get-Process | Where-Object -Property WorkingSet -gt 100MB.
- Compare the output again with the output of tasklist /FI "MEMUSAGE gt 102400" (in Windows) and ps -aux | awk -F" " '$5 > 102400' (in Linux):
- Lastly, execute this cmdlet: Stop-Computer -WhatIf. This time, there's no comparable command on either Windows or Linux.
How it works...
PowerShell works with commands like any other shell environment. Native PowerShell commands are called cmdlets. Unlike commands from other shells, PowerShell cmdlets should only serve one purpose and fulfill this purpose only. As always, there're exceptions to the rule. In some cases, command-line switches, called switch parameters, can be used to toggle additional functionality.
The first example, Get-Process, returns (Get) a list of running processes (Process). While the formatted output appears similar to that of the Windows command tasklist, PowerShell doesn't merely return text, but .NET objects.
Our second example, Get-Date, returns the current date and time as a .NET object again. In .NET, time is calculated with ticks, which are 100 nanosecond-intervals starting at 0001-01-01 00:00:00. The output is formatted depending on your operating system's culture and can be changed on demand.
The third example has you filter the output with PowerShell, which is extremely easy compared to Windows and Linux alike. Especially the endless possibilities of working with text in Linux make this a striking example. The ps command doesn't allow much filtering, so you need to rely on tools such as awk to process the text that is returned. This simple task without PowerShell requires knowledge of text processing and filtering with different tools.
The last cmdlet, Stop-Computer, demonstrates a very common parameter with many cmdlets called WhatIf. This parameter allows you to simply try a cmdlet before actually doing anything. This is an excellent way to test changes for general correctness, for example, before modifying your 10.000 Active Directory user accounts:
There's more...
There's plenty more to do and see in PowerShell—part of which will be covered in this book. Try to follow the upcoming recipes as well to find out about cmdlet discovery, the flow between cmdlets in the pipeline, and much more.
See also
- All official documentation regarding PowerShell: https://docs.microsoft.com/powershell
- Learn PowerShell Core 6.0 by David das Neves and Jan-Hendrik Peters, as a supplementary book to really learn the language: https://www.packtpub.com/networking-and-servers/learn-powershell-core-60