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

How do cmdlets work?

In contrast to native OS commands such as ps on Linux or tasklist on Windows, PowerShell uses cmdlets. These cmdlets always follow the same, simple syntax. Moreover, specifying parameters and their values always works the same with every cmdlet as well.

This section will help you to understand how cmdlets work before diving into them in later chapters.

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. Start PowerShell Core.
  2. Type New-Item -Path variable: -Name myVariable -Value "Isn't it great?.
  3. Type $myVariable. $myVariable is a variable—a temporary storage space for your data. Just executing the variable will place it on the output.
  4. Type Get-ChildItem $home *.*. This cmdlet uses no parameter names, and only parameter values. $home is a built-in variable and *.* filters for all files with a dot in the name.
  5. Type Get-ChildItem *.txt $home. Observe the error this time. You can't mix positional parameters.
  6. Type Get-ChildItem -Filter *.txt -Path $home. By using the parameter names, the cmdlet works again.
  7. Type $processName = 'powershell'. Assigning anything to a variable like this will store the result in the variable.
  8. Type Get-Process $processName.
  9. Type Get-Process $pid. As opposed to the process name, using an ID will fail the cmdlet.
  10. Type Get-Process -Id $pid. By using the correct parameter, the cmdlet works again.
  11. Type Get-Command -Syntax -Name Get-Process. Observe the syntax of the cmdlet; there's more than one way to execute a cmdlet. These are called parameter sets.

How it works...

Native PowerShell cmdlets should all follow the exact same syntax: verb-noun. The verb indicates the action and the noun indicates the recipient of that action. Whether it is a cmdlet such as New-Item or Get-Process, the syntax always follows the same principle.

With all of its different parameters used, a full cmdlet call might look like the following example:

Get-ChildItem $home -Filter *.txt -File

Get-ChildItem is the name of the cmdlet. $home is the value of a so-called positional parameter, Path. -Filter uses the parameter name, and *.txt is the value provided for that parameter. -File is something called a switch parameter, which resembles a command-line switch.

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