Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Powershell Core 6.2 Cookbook

You're reading from   Powershell Core 6.2 Cookbook Leverage command-line shell scripting to effectively manage your enterprise environment

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781789803303
Length 372 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jan-Hendrik Peters Jan-Hendrik Peters
Author Profile Icon Jan-Hendrik Peters
Jan-Hendrik Peters
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Introduction to PowerShell Core FREE CHAPTER 2. Reading and Writing Output 3. Working with Objects 4. Mastering the Pipeline 5. Importing, Using, and Exporting Data 6. Windows and Linux Administration 7. Windows Server Administration 8. Remoting and Just Enough Administration 9. Using PowerShell for Hyper-V and Azure Stack Management 10. Using PowerShell with Azure and Google Cloud 11. Accessing Web Services 12. High-Performance Scripting 13. Other Books You May Enjoy

Running PowerShell Core

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.

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:

  1. On Windows, run pwsh.exe. On Linux or macOS, run pwsh.
  2. Type your first cmdlet, Get-Process, to retrieve a list of running processes on the system and hit Enter to confirm.
  3. Compare the output of the cmdlet with the output of tasklist (in Windows) and ps (in Linux):
  1. Type Get-Date and hit Enter to confirm.
  1. Compare the output of this cmdlet with the output of date /t (in Windows) and date (in Linux):
  1. Execute the line: Get-Process | Where-Object -Property WorkingSet -gt 100MB.
  2. Compare the output again with the output of tasklist /FI "MEMUSAGE gt 102400" (in Windows) and ps -aux | awk -F" " '$5 > 102400' (in Linux):
  1. 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

You have been reading a chapter from
Powershell Core 6.2 Cookbook
Published in: Apr 2019
Publisher: Packt
ISBN-13: 9781789803303
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image