Windows PowerShell in Action
eBook - ePub

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

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

Windows PowerShell in Action

Bruce Payette, Richard Siddaway

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Windows PowerShell in Action an online PDF/ePUB?
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 Informatica & Linguaggi di programmazione. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2017
ISBN
9781638351566

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