Windows PowerShell in Action
eBook - ePub

Windows PowerShell in Action

  1. 904 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Windows PowerShell in Action

About this book

Summary Windows PowerShell in Action, Third Edition is the definitive guide to PowerShell, now revised to cover PowerShell 6.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology In 2006, Windows PowerShell reinvented the way administrators and developers interact with Windows. Today, PowerShell is required knowledge for Windows admins and devs. This powerful, dynamic language provides command-line control of the Windows OS and most Windows servers, such as Exchange and SCCM. And because it's a first-class.NET language, you can build amazing shell scripts and tools without reaching for VB or C#. About the Book Windows PowerShell in Action, Third Edition is the definitive guide to PowerShell, now revised to cover PowerShell 6. Written by language designer Bruce Payette and MVP Richard Siddaway, this rich book offers a crystal-clear introduction to the language along with its essential everyday use cases. Beyond the basics, you'll find detailed examples on deep topics like performance, module architecture, and parallel execution. What's Inside

  • The best end-to-end coverage of PowerShell available
  • Updated with coverage of PowerShell v6
  • PowerShell workflows
  • PowerShell classes
  • Writing modules and scripts
  • Desired State Configuration
  • Programming APIs and pipelines


About the Reader Written for intermediate-level developers and administrators. About the Authors Bruce Payette is codesigner and principal author of the Power-Shell language. Richard Siddaway is a longtime PowerShell MVP, author, speaker, and blogger. Table of Contents

  • Welcome to PowerShell
  • Working with types
  • Operators and expressions
  • Advanced operators and variables
  • Flow control in scripts
  • PowerShell functions
  • Advanced functions and scripts
  • Using and authoring modules
  • Module manifests and metadata
  • Metaprogramming with scriptblocks and dynamic code
  • PowerShell remoting
  • PowerShell workflows
  • PowerShell Jobs
  • Errors and exceptions
  • Debugging
  • Working with providers, files, and CIM
  • Working with.NET and events
  • Desired State Configuration
  • Classes in PowerShell
  • The PowerShell and runspace APIs
  • Appendix - PowerShell 6.0 for Windows, Linux, and MacOS

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Windows PowerShell in Action by Bruce Payette,Richard Siddaway in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.

Information

Chapter 1. Welcome to PowerShell

This chapter covers
  • Core concepts
  • Aliases and elastic systems
  • Parsing and PowerShell
  • Pipelines
  • Formatting and output
Vizzini: Inconceivable!
Inigo: You keep on using that word. I do not think it means what you think it means.
William Goldman, The Princess Bride
It may seem strange to start by welcoming you to PowerShell when PowerShell is ten years old (at the time of writing), is on its fifth version, and you’re reading the third edition of this book.
Note
PowerShell v6 is under development as we write this. The appendix covers the changes that this new version will introduce.
In reality the adoption of PowerShell is only now achieving significant momentum, meaning that to many users PowerShell is a new technology and the three versions of PowerShell subsequent to this book’s second edition contain many new features. Welcome to PowerShell.
Note
This book is written using PowerShell v5. It’ll be noted in the text where earlier versions are different, or work in a different manner. We’ll also document when various features were introduced to PowerShell or significantly modified between versions. We treat v5 and v5.1 together as v5 as the differences are relatively minor.
Windows PowerShell is the command and scripting language from Microsoft built into all versions of Windows since Windows Server 2008. Although PowerShell is new and different (or has new features you haven’t yet explored), it’s been designed to make use of what you already know, making it easy to learn. It’s also designed to allow you to learn a bit at a time.
Running PowerShell commands
You have two choices for running the examples provided in this book. First is to use the PowerShell console. This provides a command-line interface. It’s the tool of choice for interactive work.
The second choice is the PowerShell Integrated Scripting Environment (ISE). The ISE supplies an editing pane plus a combined output and interactive pane. The ISE is the tool of choice when developing scripts, functions, and other advanced functionality.
The examples in the book will be written in a way that allows pasting directly into either tool.
Third-party tools exist, such as those supplied by Sapien, but we’ll only consider the native tools in this book.
Starting at the beginning, here’s the traditional “Hello world” program in PowerShell:
'Hello world.'
But “Hello world” itself isn’t interesting. Here’s something a bit more complicated:
Get-ChildItem -Path $env:windir\*.log | Select-String -List error | Format-Table Path,LineNumber –AutoSize
Although this is more complex, you can probably still figure out what it does. It searches all the log files in the Windows directory, looking for the string “error”, and then prints the full name of the matching file and the matching line number. “Useful, but not special,” you might think, because you can easily do this using cmd.exe on Windows or bash on UNIX. What about the “big, really big” thing? Well, how about this example:
([xml] [System.Net.WebClient]::new(). DownloadString('http://blogs.msdn.com/powershell/rss.aspx')). RSS.Channel.Item | Format-Table title,link
Now we’re getting somewhere. This script downloads the RSS feed from the PowerShell team blog and then displays the title and a link for each blog entry. By the way, you weren’t expected to figure out this example yet. If you did, you can move to the head of the class!
One last example:
using assembly System.Windows.Forms using namespace System.Windows.Forms $form = [Form] @{ Text = 'My First Form' } $button = [Button] @{ Text = 'Push Me!' Dock = 'Fill' } $button.add_Click{ $form.Close() } $form.Controls.Add($button) $form.ShowDialog()
This script uses the Windows Forms library (WinForms) to build a GUI that has a single button displaying the text “Push Me!” Figure 1.1 shows the window this script creates.
Figure 1.1. When you run the code from the example, this window will be displayed.
When you click the button, it closes the form and exits the script. With this you go from "Hello world" to a GUI application in less than two pages.
Let’s come back down to Earth for a minute. The intent of chapter 1 is to set the stage for understanding PowerShell—what it is, what it isn’t, and, almost as important, why the PowerShell team made the decisions they made in designing the PowerShell language. Chapter 1 covers the goals of the project, along with some of the major issues the team faced in trying to achieve those goals. First, a philosophical digression: while under development, from 2002 until the first public release in 2006, the codename for this project was Monad. The name Monad comes from The Monadology by Gottfried Wilhelm Leibniz, one of the inventors of calculus. Here’s how Leibniz defined the Monad:
The Monad, of which we shall here speak, is nothing but a simple substance, which enters into compounds. By “simple” is meant “without parts.”
Gottfried Wilhelm Leibniz, The Monadology (translated by Robert Latta)
In The Monadology, Leibniz describes a world of irreducible components from which all things could be composed. This captures the spirit of the project: to create a toolkit of simple pieces you compose to create complex solutions.

1.1. What is PowerShell?

What is PowerShell, and what can you do with it? Ask a group of PowerShell users and you’ll get different answers:
  • PowerShell is a command-line shell.
  • PowerShell is a scripting environment.
  • PowerShell is an automation engine.
These are all part of the answer. We prefer to say PowerShell is a tool you can use to manage your Microsoft-based machines and applications that programs consistency into your management process. The tool is attractive to administrators and developers in that it can span the range of command line, simple and advanced scripts, to real programs.
Note
If you take this to mean PowerShell is the ideal DevOps tool for the Microsoft platform, then congratulations—you’ve got it in one.
PowerShell draws heavily from existing command-line shell and scripting languages, but the language, runtime, and subsequent additions, such as PowerShell Workflows and Desired State Configuration, were designed from scratch to be an optimal environment for the modern Windows operating system.
Most people are introduced to PowerShell through its interactive aspects. Let’s refine our definitions of shell and scripting.

1.1.1. Shells, command lines, and scripting languages

In the previous section we called PowerShell a command-line shell. You may be asking, what’s a shell? And how’s it different from a command interpreter? What about scripting languages? If you can script in a shell language, doesn’t that make it a scripting language? In answering these questions, let’s start with shells.
Defining a shell can be tricky because pretty much everything at Microsoft has something called a shell. Windows Explorer is a shell. Visual Studio has a component called a shell. Heck, even the Xbox has somet...

Table of contents

  1. Copyright
  2. Brief Table of Contents
  3. Table of Contents
  4. Praise for the Second Edition
  5. Praise for the First Edition
  6. Preface
  7. Acknowledgments
  8. About this Book
  9. About the Cover Illustration
  10. Chapter 1. Welcome to PowerShell
  11. Chapter 2. Working with types
  12. Chapter 3. Operators and expressions
  13. Chapter 4. Advanced operators and variables
  14. Chapter 5. Flow control in scripts
  15. Chapter 6. PowerShell functions
  16. Chapter 7. Advanced functions and scripts
  17. Chapter 8. Using and authoring modules
  18. Chapter 9. Module manifests and metadata
  19. Chapter 10. Metaprogramming with scriptblocks and dynamic code
  20. Chapter 11. PowerShell remoting
  21. Chapter 12. PowerShell workflows
  22. Chapter 13. PowerShell Jobs
  23. Chapter 14. Errors and exceptions
  24. Chapter 15. Debugging
  25. Chapter 16. Working with providers, files, and CIM
  26. Chapter 17. Working with .NET and events
  27. Chapter 18. Desired State Configuration
  28. Chapter 19. Classes in PowerShell
  29. Chapter 20. The PowerShell and runspace APIs
  30. Appendix. PowerShell 6.0 for Windows, Linux, and macOS
  31. Index
  32. List of Figures
  33. List of Tables
  34. List of Listings