Book Image

Powershell Core 6.2 Cookbook

By : Jan-Hendrik Peters
Book Image

Powershell Core 6.2 Cookbook

By: Jan-Hendrik Peters

Overview of this book

This book will follow a recipe-based approach and start off with an introduction to the fundamentals of PowerShell, and explaining how to install and run it through simple examples. Next, you will learn how to use PowerShell to access and manipulate data and how to work with different streams as well. You will also explore the object model which will help with regard to PowerShell function deployment. Going forward, you will get familiar with the pipeline in its different use cases. The next set of chapters will deal with the different ways of accessing data in PowerShell. You will also learn to automate various tasks in Windows and Linux using PowerShell Core, as well as explore Windows Server. Later, you will be introduced to Remoting in PowerShell Core and Just Enough Administration concept. The last set of chapters will help you understand the management of a private and public cloud with PowerShell Core. You will also learn how to access web services and explore the high-performance scripting methods. By the end of this book, you will gain the skills to manage complex tasks effectively along with increasing the performance of your environment.
Table of Contents (14 chapters)

Performing read-only operations

Very often, PowerShell is used to gather data for reporting purposes, exporting and viewing configurations, and more. This is generally accomplished using the various Get cmdlets. In Chapter 2, Reading and Writing Output, we'll then see how to further process the gathered data.

Since the Get cmdlets won't change anything on your system, this is a great way to discover what PowerShell has to offer.

Getting ready

In order to follow this recipe, you should have completed the installation of PowerShell Core for your operating system. Some cmdlets are Windows-specific and require a Windows operating system.

How to do it...

Please perform the following steps:

  1. On a Windows system, you can use many built-in cmdlets with PowerShell Core. Try Import-Module Storage -SkipEditionCheck.
  2. On a Windows system, type Get-Disk to list all disks. The result should look similar to the following:
  1. On a Windows system, use Get-Disk -Number 0 | Get-Partition to retrieve the partitions on the first disk:
  1. On any system, try the Get-Uptime cmdlet to calculate the system uptime.
  2. Use Get-Culture and Get-UICulture to view the current language settings.
  3. Review the result of Get-PackageProvider. On a Windows system, additional providers are visible.

How it works...

PowerShell provides access to several different data sources by means of the Get cmdlets. After retrieving the data, PowerShell wraps it into an object model to allow you to store, display, filter, and process the data.

The data sources can be anything from services, event logs, and files to functions and variables. Every item that's retrieved is called an object and will have different properties and methods, which we will see later on.

There's more...

Just explore Get-Command -Verb Get to find all read-only cmdlets and simply have a look at what the return values are. There's no harm in trying!

On your way to building a perpetuum mobile? Make PowerShell execute all Get cmdlets for some fun:

$ErrorActionPreference = 'SilentlyContinue'
Get-Command -Verb Get | ForEach-Object { & $_ }

The ampersand operator will invoke an expression. By iterating over each Get cmdlet that is returned by Get-Command, we try to read everything we can get our hands on. Please be aware that this will take some time.