Windows PowerShell is a successful, compelling, and integrated tool that all good .NET developers, IT pros, and anyone working with Windows should have in their toolboxes.
It can be used for making unit tests more powerful, scripting tasks such as reading XML or data imports, providing integration points in your .NET applications for end users to customize or extend using their own scripts, and defining little languages to express readable and concise business rules.
PowerShell simplifies your life, opening doors not previously accessible to you, by providing a .NET-based scripting language filled with useful features and application programming interfaces (APIs) for all the common programming tasks you take on daily.
You’ll quickly learn the basic concepts using the interactive command line, and you’ll move rapidly to creating scripts and embedding PowerShell into your existing .NET applications.
This book is for anyone who wants to know more about PowerShell. If you’re serious about PowerShell, it’s a must read. This book walks you through what is possible with PowerShell—helping you answer questions such as “can this be done better, faster, or simpler, or can I make it repeatable?”—and planting the seeds for you to creatively apply this new distributed automation platform on your own.
This book is not a beginner’s guide to PowerShell. If you are an experienced developer or IT pro, this book gives you insight into what PowerShell can do.
The examples in this book are runnable out of the box. You can study how and what the scripts do—this is one of the tried-and-true ways of learning a new paradigm. While some examples include C# .NET, it is not required that you understand C#.
The examples are self-contained. Run them; see what they do. Then you can pull them apart, tweak them, and incorporate them into your PowerShell and .NET solutions.
Chapter 1 gives an overview of the platform and answers the question “Why PowerShell?”
Chapter 2 steps you through getting PowerShell prepped for running.
Chapter 3 offers a walkthrough of things you probably didn’t even know the PowerShell platform could do.
Chapter 4 covers writing a template engine and using the new PowerShell v3 abstract syntax tree interface to extract information from PowerShell scripts.
Chapter 5 kicks it up a notch and shows you how easy it is to provide scripting abilities for your C# (WPF) apps by embedding PowerShell into them.
Chapter 6 demonstrates PowerShell’s excellent capabilities for working with the Internet. JSON, XML, HTTP, Twitter? No problem.
Chapter 7 demonstrates how PowerShell is based on .NET. Want to build GUIs with less code? This is the chapter for you.
Chapter 8 further explores PowerShell’s relationship to .NET and shows you how to leverage this seamless integration with other Microsoft frameworks.
Chapter 9 covers one of my favorite topics—building “little languages”—and shows how PowerShell makes this easy. Whether you prefer domain-specific languages (DSL) or domain-specific vocabularies (DSV), you’ll want to check out what PowerShell has to offer.
Chapter 10 shows you how to really leverage applications like Microsoft Excel and by extension, Microsoft COM (Component Object Model) applications.
Chapter 11 is an excursion through some of the new and exciting features of PowerShell v3, set to ship with Windows 8 and Windows Server 2012, and available in beta for Windows 7.
Appendix A is all about programmer productivity. This is a PowerShell sweet spot, and this chapter shows you how to get the most out of the platform.
Appendix B shows you how to enable PowerShell v2 to load and work with .NET 4.0 DLLs. This is the default mode in PowerShell v3.
The following typographical conventions are used in this book:
Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl).
Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.
Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “PowerShell for Developers by Douglas Finke (O’Reilly). Copyright 2012 Douglas Finke, 978-1-4493-2270-0.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at firstname.lastname@example.org.
The code examples in the following chapters are available for download from GitHub at https://github.com/dfinke/powershell-for-developers.
Please address comments and questions concerning this book to the publisher:
|O’Reilly Media, Inc.|
|1005 Gravenstein Highway North|
|Sebastopol, CA 95472|
|(800) 998-9938 (in the United States or Canada)|
|(707) 829-0515 (international or local)|
|(707) 829-0104 (fax)|
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
To comment or ask technical questions about this book, send email to:
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.
Writing a book is an interesting journey. Now that it’s completed, looking back over the last several months I’m amazed at how lucky I’ve been to come in contact with so many terrific people.
I’d like to thank my editor at O’Reilly, Rachel Roumeliotis, who was absolutely amazing to work with.
Thank you to Elizabeth, my daughter, who has just finished another year at university and continues to be my inspiration.
I was fortunate to have three great guys as reviewers for my book. They spent countless hours providing feedback and examples, researching specific content, offering lots of encouragement, and engaging with me in great discussions about PowerShell.
A special thanks to Daniel Moore. His passion for computing has earned him the nickname Beaver (as in “eager beaver”). He jumps in deep-end first and starts building dams like nobody’s business. He’s responsible for the WPF GUI in Chapter 5, a.k.a. the “Beaver Music application.” He helped save me tons of time prepping the code for NuGet and the other examples for GitHub. Thanks, Daniel!
Thank you very much, Aleksandar Nikolic´ and Steve Murawski, fellow PowerShell MVPs and cofounders of PowerShell Magazine (http://www.powershellmagazine.com/).
Aleksandar’s incredible attention to detail was a significant asset in helping to finalize the book. He has a passion for PowerShell and is extremely generous with the time that he spends with the PowerShell community. Catch him at the next PowerShell Deep Dive.
Steve’s depth of knowledge on PowerShell let him plow through these chapters and provide great feedback throughout the process.
When Steve signed on to review the book, his family was about to increase by one. He reviewed the chapters, did speaking gigs (including PowerShell Deep Dive), went to his day job, and took care of a newborn. Makes me tired just writing about it.
Gentlemen, it was an honor and privilege working with you.
Allyson Chisholm—you have my heart.
Sal Mangano—fellow author, how you wrote a 1,000-page book is beyond me.
Thank you to this gang, a group of smart, supportive people whom I continue to learn from: Jeffrey Snover, James Brundage, Bruce Payette, Lee Holmes, Jason Shirk, Ed Wilson, Davin Tanabe, Jason Dolinger, Ravikanth Chaganti, Shay Levy, Ajay Kalras, Joel Bennett, Oisín Grehan, Keith Hill, Karl Prosser, Will Steele, Justin Rich, Lance Arlaus, Peter Coates, Ronald Lindtag, Sivabalan Muthukumar, Bailey Ling, Josh Einstein, and last but not least, Caleb and Ebony Finke (the furry ones).