Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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 2. Reading and Writing Output FREE CHAPTER 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

Discovering the environment

PowerShell Core has plenty of built-in variables that give you immediate information about the environment you are working with. The following recipe will show you the most important ones.

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

Please perform the following steps:

  1. Review the output of $PSVersionTable. With this variable, you'll always know which version and edition you are running.
  2. Try to execute Set-Location $PSHome; Get-ChildItem. This folder contains all PowerShell binaries necessary to run the shell.
  3. Have a look at the value of $pid. This variable always points to your own PowerShell process.
  4. Try running the Get-Item DoesNotExist cmdlet and afterward, view the contents of $Error. This variable collects errors that happen in your session. Not all errors collected here have been visible on the CLI.
  5. Try the following: $true = $false. You'll be pleasantly surprised that these variables are so-called constants and can't be changed.
  6. Have a look at the output of Get-Process | Format-Table Name,Threads. You'll notice that the threads always seem to stop at four elements.
  7. Display the contents of the variable, $FormatEnumerationLimit. The value of four isn't a coincidence. This variable governs how list output is formatted.
  8. Have a look at $PSScriptRoot. For some reason, this variable is empty. The reason is that this variable is only set when a script is executed. It then will point to the directory containing the script. $PSCommandPath will contain the entire script path.
  1. Run the following command: Set-Content -Path ~/test.ps1 -Value '$PSScriptRoot;$PSCommandPath'; ~/test.ps1. Examine the output; the first line contains the script directory, whereas the second line will show the full script path you executed.
  2. Lastly, try Get-Variable *Preference. These variables control the behavior of PowerShell regarding errors, warnings and more. In Chapter 2, Reading and Writing Output, we'll have a close look at those.

How it works...

Each time a new PowerShell session is started, a bunch of variables is registered and filled. You can always rely on those variables to exist and be present in your scripts. Many of those variables contain preferences for cmdlets, formatting, and output.

There's more...

In the following chapters, there'll be more. We'll continue using the built-in variables for different purposes.

lock icon The rest of the chapter is locked
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