Windows PowerShell 3 - Pearsoncmg

Transcription

Windows PowerShell 3.0First StepsEd Wilson

Copyright 2013 by Ed WilsonAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by anymeans without the written permission of the publisher.ISBN: 978-0-7356-8100-21 2 3 4 5 6 7 8 9 LSI 8 7 6 5 4 3Printed and bound in the United States of America.Microsoft Press books are available through booksellers and distributors worldwide. If you need support relatedto this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think ofthis book at soft and the trademarks listed at ctualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property oftheir respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, andevents depicted herein are fictitious. No association with any real company, organization, product, domain name,email address, logo, person, place, or event is intended or should be inferred.This book expresses the author’s views and opinions. The information contained in this book is provided withoutany express, statutory, or implied warranties. Neither the authors, Microsoft Corporation,nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directlyor indirectly by this book.Acquisitions and Developmental Editor: Michael BolingerProduction Editor: Melanie YarbroughEditorial Production: Box Twelve CommunicationsTechnical Reviewer: Brian WilhiteIndexer: Box Twelve CommunicationsCover Design: Twist Creative SeattleCover Composition: Ellie VolckhausenIllustrator: Rebecca Demarest

To Teresa, my soul mate.—Ed Wilson

Contents at a glanceForewordIntroductionxvxviiChapter 1Overview of Windows PowerShell 3.01Chapter 2Using Windows PowerShell cmdlets21Chapter 3Filtering, grouping, and sorting41Chapter 4Formatting output53Chapter 5Storing output69Chapter 6Leveraging Windows PowerShell providers79Chapter 7Using Windows PowerShell remoting99Chapter 8Using WMI113Chapter 9Using CIM127Chapter 10Using the Windows PowerShell ISE141Chapter 11Using Windows PowerShell scripts153Chapter 12Working with functions183Chapter 13Debugging scripts203Chapter 14Handling errors217Appendix AWindows PowerShell FAQ229Appendix BWindows PowerShell 3.0 coding conventions239Index247

ContentsForewordxvIntroductionChapter 1xviiOverview of Windows PowerShell 3.01Understanding Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Working with Windows PowerShell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Security issues with Windows PowerShell4Using Windows PowerShell cmdlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6The most common verb: Get6Supplying options for cmdlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Using single parameters13Introduction to parameter sets16Using command-line utilities18Working with Help options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Chapter 2Using Windows PowerShell cmdlets21Understanding the basics of cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Common Windows PowerShell parameters22Starting the Windows PowerShell transcript24Stopping and reviewing the Windows PowerShell transcript25Searching the Help topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Using the Get-Help cmdlet26Using the About conceptual Help topics29What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can continually improve ourbooks and learning resources for you. To participate in a brief online survey, please visit:www.microsoft.com/learning/booksurvey/vii

Using the Get-Command to find cmdlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Using the Get-Member cmdlet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Exploring property members34Using the Show-Command cmdlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Setting the Script Execution Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Creating a basic Windows PowerShell profile. . . . . . . . . . . . . . . . . . . . . . . . 37Determining if a Windows PowerShell profile exists38Creating a new Windows PowerShell profile38Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Chapter 3Filtering, grouping, and sorting41Introduction to the pipeline. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Sorting output from a cmdlet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Grouping output after sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Grouping information without element data45Filtering output from one cmdlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Filtering by date47Filtering to the left49Filtering output from one cmdlet before sorting. . . . . . . . . . . . . . . . . . . . . 50Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Chapter 4Formatting output53Creating a table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Choosing specific properties in a specific order54Controlling the way the table displays55Creating a list. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Choosing properties by name59Choosing properties by wildcard59Creating a wide display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61viiiContentsUsing the -AutoSize parameter to configure the output61Customizing the Format-Wide output62

Creating an output grid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Sorting output by using the column buttons64Filtering output by using the filter box66Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Chapter 5Storing output69Storing data in text files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69Redirect and append70Redirect and overwrite71Controlling the text file72Storing data in .csv files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73No type information73Using type information75Storing data in XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76The problem with complex objects76Using XML to store complex objects76Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Chapter 6Leveraging Windows PowerShell providers79Understanding Windows PowerShell providers . . . . . . . . . . . . . . . . . . . . . . 80Understanding the Alias provider80Understanding the Certificate provider82Understanding the Environment provider85Understanding the File System provider86Understanding the Function provider88Understanding the Registry provider89Understanding the Variable provider96Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Chapter 7Using Windows PowerShell remoting99Using Windows PowerShell remoting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Classic remoting99Contentsix

Configuring Windows PowerShell remoting. . . . . . . . . . . . . . . . . . . . . . . . 101Running commands103Creating a persisted connection107Troubleshooting Windows PowerShell remoting. . . . . . . . . . . . . . . . . . . . 110Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Chapter 8Using WMI113Understanding the WMI Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113Working with objects and namespaces114Listing WMI providers114Working with WMI classes115Querying WMI: The basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Tell me everything about everything120Tell me selected things about everything122Tell me everything about some things123Tell me selected things about some things125Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Chapter 9Using CIM127Using CIM cmdlets to explore WMI classes. . . . . . . . . . . . . . . . . . . . . . . . . 127Using the classname parameter128Finding WMI class methods128Filtering classes by qualifier130Reducing returned properties and instances133Cleaning up output from the command134Working with associations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140Chapter 10 Using the Windows PowerShell ISE141Running the Windows PowerShell ISE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141xContentsNavigating the Windows PowerShell ISE142Working with the Script pane145Tab expansion and Intellisense146

Working with Windows PowerShell ISE snippets . . . . . . . . . . . . . . . . . . . . 148Using Windows PowerShell ISE snippets to create code148Creating new Windows PowerShell ISE snippets149Removing user-defined Windows PowerShell ISE snippets150Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Chapter 11 Using Windows PowerShell scripts153Why write Windows PowerShell scripts?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Scripting fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Running Windows PowerShell scripts155Enabling Windows PowerShell scripting support156Transitioning from command line to script157Running Windows PowerShell scripts159Understanding variables and constants160Using the While statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Constructing the While statement162A practical example of using the While statement164Using special features of Windows PowerShell164Using the Do While statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Using the range operator166Operating over an array166Casting to ASCII values167Using the Do Until statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Using the Windows PowerShell Do Loop statement168Using the For statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Creating a For Loop170Using the ForEach statement172Exiting the ForEach statement early174Using the If statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Using assignment and comparison operators177Evaluating multiple conditions178Contentsxi

Using the Switch statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Using the basic Switch statement180Controlling matching behavior182Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Chapter 12 Working with functions183Understanding functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Using a type constraint190Using multiple input parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Using functions to encapsulate business logic . . . . . . . . . . . . . . . . . . . . . . 194Using functions to provide ease of modification . . . . . . . . . . . . . . . . . . . . 196Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Chapter 13 Debugging scripts203Understanding debugging in Windows PowerShell. . . . . . . . . . . . . . . . . . 203Debugging the script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Setting breakpoints204Setting a breakpoint on a line number204Setting a breakpoint on a variable206Setting a breakpoint on a command209Responding to breakpoints211Listing breakpoints213Enabling and disabling breakpoints215Deleting breakpoints215Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216Chapter 14 Handling errors217Handling missing parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217xiiContentsCreating a default value for the parameter218Making the parameter mandatory219

Limiting choices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Using PromptForChoice to limit selections220Using Test-Connection to identify accessible computers222Using the contains operator to examine contents of an array223Handling missing rights. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Attempting and failing226Checking for rights and exiting gracefully226Using Try/Catch/Finally. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Appendix A Windows PowerShell FAQ229Appendix B Windows PowerShell 3.0 coding conventions239General script construction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Include functions in the script that uses the functions239Use full cmdlet names and full parameter names240Use Get-Item to convert path strings to rich types241General script readability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241Formatting your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242Working with functions244Creating template files244Writing your own functions245Variables, constants, and naming245Index247What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can continually improve ourbooks and learning resources for you. To participate in a brief online survey, please tsxiii

ForewordThere are many reasons to get started with automation. For me it was a little turtle from aprogram called LOGO. Of course, at the time I had no idea I was learning programming.I was just a kid in elementary school having fun, drawing little pictures. Years later, I becamean IT administrator and developed an aversion to tedious tasks, such as manually copying afile to 100 remote servers. I started automating because I just couldn’t stand the thought ofrepeating monotonous tasks over and over again. It took a while before I connected the dotsand realized that the little turtle had paved the way for a career focused on using and teaching automation.Windows PowerShell has really hit a sweet spot with automation in the Windows universe,balancing powerful and far-reaching capabilities while remaining simple enough that someone without deep technical expertise can start taking advantage of it quickly. Though Windows PowerShell can be a simple automation environment, it has nuances that can make it abit tricky to really master, akin to driving a car with a manual transmission. It might be trickyto get started, but once the car is moving in first gear, the rest comes pretty easily. Ed Wilsonhas done a wonderful job in this book getting you started in Windows PowerShell, providingsimple, prescriptive guidance to get you into first gear quickly.As a Senior Premier Field Engineer and a Windows PowerShell Technology Lead for Microsoft Services, I spend most of my days in front of Microsoft’s customers trying to teach themWindows PowerShell and hopefully getting them to love Windows PowerShell as much as Ido. In every class I teach, I can’t stress enough the return on investment (ROI) you get fromlearning Windows PowerShell. It never ceases to amaze me how once you grasp the coreconcepts of Windows PowerShell, you can apply them over and over again to get so muchbusiness value and personal satisfaction.One point I try to make during every class I teach is that the words “Windows PowerShell” and“scripting” can most definitely be mutually exclusive. Technically speaking, Windows PowerShellone-liners are still “scripts,” but to me they strike a nice balance between the creation of solutionsand the need for developer-oriented skills. One-liners are usually very task-oriented and logicallysimple, yet they can accomplish a staggering amount of automation. Those who are just gettingstarted with Windows PowerShell will find that they can become great at Windows PowerShell without writing scripts. Throughout much of this book, Ed has focused on the conceptsand simplicity of Windows PowerShell. He doesn’t talk directly about scripting until late in thebook. Ultimately, scripting and tool-making become parts of the advanced user’s skill set, butyou can go a long way before that needs to happen.No matter how diverse the skill set of my students, there is something for everyone in myclassroom. Windows PowerShell has been created in such a way that it can be fun and effective for everyone from the IT novice to the expert developer. For example, the fact that it isxv

fully object-based and sits on top of the .NET Framework is a detail that pure beginners mighthave no knowledge of. They can go about their Windows PowerShell days simply runningcommands, never really digging into the object model, but still implement valuable automation. The day they learn about objects, they can start to unlock so much more. The fact thatWindows PowerShell can appeal to such diverse skills levels simultaneously is amazing to me.When I really think about the value of Windows PowerShell and why someone new to itshould dive right in, I think about the fundamental comparison of “creation” vs. “operation.”By over-simplifying the roles in IT, you can see a dividing line between developers and administrators. Developers are creating solutions, and administrators are managing the design,deployment, and operation of the systems used in the process. Windows PowerShell canbridge the dividing gap to link it all together. It also allows administrators to create automation solutions without needing a true developer. There are enough elements in the WindowsPowerShell language that hide and simplify the true complexity that lurks under the surface,allowing IT pros to be more effective and valuable in the workplace. Learning Windows PowerShell is an incredibly powerful tool that will truly make you more valuable to your businessand often make your life easier in the process.Ed “The Scripting Guy” Wilson is what some people call a “PowerShellebrity.” He’s a superstar in the Windows PowerShell world, has extensive scripting experience, and is one ofthe most energetic and passionate people I have ever met. I am grateful that Ed writes thesebooks because it allows so many people access to his extensive experience and knowledge.This book is such a concise and easy way to get started with Windows PowerShell, I can’timagine putting it down if I were a beginner. Whether you have already started your Windows PowerShell journey or are just getting started, this book will help define your next stepswith Windows PowerShell.—Gary SiepsertSenior Premier Field Engineer (PFE)Microsoft Corporationxvi Foreword

IntroductionGary said nearly everything I wanted to include in the Introduction. I designed this bookfor the complete beginner, and you should therefore read the book from beginning toend. If you want a more reference oriented book, you should check out my PowerShell BestPractices books, or even PowerShell 3.0 Step by Step. Actually, the Step by Step book is notreally a reference, but a hands-on learning guide. It is, ideally, the book you graduate to onceyou have completed this one. For your daily dose of PowerShell, you should check out my HeyScripting Guy blog at www.ScriptingGuys.com/blog. I post new content there twice a day.System RequirementsHardware RequirementsYour computer should meet the following minimum hardware requirements: 2.0 GB of RAM (more is recommended) 80 GB of available hard disk space Internet connectivitySoftware RequirementsTo complete the exercises in this book, you should have Windows PowerShell 3.0 installed: You can obtain Windows PowerShell 3.0 from the Microsoft Download Center bydownloading the Windows Management Framework and installing it on either Windows 7 Service Pack 1, Windows Server 2008 R2 SP1, or Windows Server 2008 ServicePack 2.Windows PowerShell 3.0 is already installed on Windows 8 and on Windows Server2012. You can obtain evaulation versions of those operating systems from ter/hh699156.aspx?ocid enter/hh670538.aspx?wt.mc id TEC 108 1 4 The section on Active Directory requires access to Active Directory Domain Services.For those examples, ensure you have access to Windows Server 2012.For the chapter on Exchange server, you need access to a server running MicrosoftExchange Server 2013. You can obtain an evaluation version of that from ter/hh973395.aspxxvii

AcknowledgmentsMany people contributed the success of this book. The first person is Teresa Wilson, aka "TheScripting Wife." She is always my first reader, and nothing leaves the house without her approval. Second, I must mention my tech reviewer, Brian Wilhite, who did a great job of catchingbugs, errors, and things that are misleading. I also want to thank the Charlotte PowerShell UserGroup whose questions, comments, and the like contributed in a significant way to the book.I kept them in mind as I wrote. I also want to thank Michael Bolinger and Melanie Yarbroughfrom O'Reilly for doing a great job seeing this project to completion.Support & FeedbackThe following sections provide information on errata, book support, feedback, and contactinformation.ErrataWe have made every effort to ensure the accuracy of this book and its companion content.Any errors that have been reported since this book was published are listed on our MicrosoftPress site:http://aka.ms/WinPS3FS/errataIf you find an error that is not already listed, you can report it to us through the samepage.If you need additional support, please email Microsoft Press Book Support atmspinput@microsoft.com.Please note that product support for Microsoft software is not offered through the addresses above.We Want to Hear from YouAt Microsoft Press, your satisfaction is our top priority, and your feedback our most valuableasset. Please tell us what you think of this book at:http://www.microsoft.com/learning/booksurveyThe survey is short, and we read every one of your comments and ideas. Thanks in advance for your input!Stay in TouchLet us keep the conversation going! We are on Twitter: http://twitter.com/MicrosoftPressxviii Introduction

CHAPTER 1Overview of WindowsPowerShell 3.0 Understanding Windows PowerShell Working with Windows PowerShell Using Windows PowerShell cmdlets Supplying options for cmdlets Working with Help optionsWhen you first start Windows PowerShell, whether it is the Windows PowerShell console or the Windows PowerShell Integrated Scripting Environment (ISE), the blankscreen simply waits for your command. The problem is there are no hints as to what thatcommand might be. There are no wizards or other Windows types of features to guide youin using the shell.The name is Windows PowerShell for two reasons: It is a shell, and it is powerful. It is amistake to think that Windows PowerShell is simply a scripting language because it is muchmore than that. In the same way, it is a mistake to think that Windows PowerShell is limitedto running only a few cmdlets. Through scripting, it gains access to the entire realm of management technology available in the Windows world.This chapter introduces you to Windows PowerShell and illustrates the incredible poweravailable to you from this flexible and useful management tool.Understanding Windows PowerShellWindows PowerShell comes in two flavors. The first is an interactive console (similar toa KORN or BASH console in the UNIX world) built into the Windows command prompt.The Windows PowerShell console makes it simple to type short commands and to receivesorted, filtered, and formatted results. These results easily display to the console but alsocan redirect to .xml, .csv, or text files. The Windows PowerShell console offers several advantages such as speed, low memory overhead, and a comprehensive transcription service thatrecords all commands and command output.1

The other flavor of Windows PowerShell is the Windows PowerShell ISE. The WindowsPowerShell ISE is an Integrated Scripting Environment, but this does not mean you must useit to write scripts. In fact, many Windows PowerShell users like to write their code in the Windows PowerShell ISE to take advantage of syntax coloring, drop-down lists, and automaticparameter revelation features.In addition, the Windows PowerShell ISE has a feature called Show Command Add-Onthat allows you to use a mouse to create Windows PowerShell commands from a graphicalenvironment. Once you create the command, the command either runs directly or is addedto the Script pane. The choice is up to you. For more information about using the WindowsPowerShell ISE, see Chapter 10, “Using the Windows PowerShell ISE.”NOTE When I work with single commands, for simplicity I show the command and resultsfrom within the Windows PowerShell console. But keep in mind that all commands alsorun from within the Windows PowerShell ISE. Whether the command runs in the WindowsPowerShell console, in the Windows PowerShell ISE, as a scheduled task, or as a filter forGroup Policy, Windows PowerShell is Windows PowerShell is Windows PowerShell. In itsmost basic form, a Windows PowerShell script is simply a collection of Windows PowerShellcommands.Working with Windows PowerShellWindows PowerShell 3.0 is included on Windows 8 and Windows Server 2012. On Windows8, you need only type the first few letters of the word PowerShell in the Start window beforeWindows PowerShell appears as an option. Figure 1-1 illustrates this point. I typed only powin the Search box before the Start window offered Windows PowerShell as an option.2CHAPTER 1Overview of Windows PowerShell 3.0

FIGURE 1-1 Typing in the Start window opens the Search window highlighting the Windows PowerShellconsole.Because navigating to the Start window and typing pow each time I want to launch Windows PowerShell is a bit cumbersome, I prefer to pin shortcuts to the Windows PowerShellconsole (and the Windows PowerShell ISE) to both the Start window and the Windows taskbar. This technique of pinning shortcuts to the applications, as shown in Figure 1-2, providessingle-click access to Windows PowerShell from wherever I might be working.FIGURE 1-2 By right-clicking the Windows PowerShell icon in the Search results box, the Pin to Start andthe Pin to taskbar options appear.Working with Windows PowerShellCHAPTER 13

On Windows Server 2012, it is unnecessary to find the icon by using the Search box on theStart window because an icon for the Windows PowerShell console exists by default on thetaskbar of the desktop.NOTE The Windows PowerShell ISE (the script editor) does not exist by default on Win-dows Server 2012. You need to add the Windows PowerShell ISE as a feature. I show how touse the Windows PowerShell ISE in Chapter 10, “Using the Windows Powershell ISE.”Security issues with Windows PowerShellThere are two ways to launch Windows PowerShell: as an administrator or as a normal, ornon-elevated, user. As a best practice, start Windows PowerShell with minimum rights. OnWindows 7 and Windows 8, this means simply clicking on the Windows PowerShell icon. Itopens as a non-elevated user, even if you are logged on with administrator rights. On Windows Server 2012, Windows PowerShell automatically launches with the rights of the currentuser. Therefore, if you are logged on as a domain administrator, the Windows PowerShell console launches with domain administrator rights.Running as a non-elevated userBecause Windows PowerShell adheres to Windows security constraints, a user of WindowsPowerShell cannot do anything the user account does not have permission to do. Therefore, ifyou are a non-elevated user, you do not have rights to perform tasks such as installing printerdrivers, reading from the Security Log, or changing the system time.If you are an administrator on a local Windows 7 or Windows 8 computer and you do notlaunch Windows PowerShell with administrator rights, you will get errors when you attemptto take certain actions, such as viewing the configuration of your disk drives. The followingexample shows the command and associated error:PS C:\ get-diskget-disk : Access to a CIM resource was not available to the client.At line:1 char:1 get-disk CategoryInfo: PermissionDenied: (MSFT Disk:ROOT/Microsoft/Windows/Storage/MSFT Disk) [Get-Disk], CimException FullyQualifiedErrorId : MI RESULT 2,Get-Disk4CHAPTER 1Overview of Windows Powe

Contents at a glance Foreword xv Introduction xvii ChAPTER 1 Overview of Windows PowerShell 3.0 1 ChAPTER 2 Using Windows PowerShell cmdlets 21 ChAPTER 3 Filtering, grouping, and sorting 41 ChAPTER 4 Formatting output 53 ChAPTER 5 Storing output 69 ChAPTER 6 Leveraging Windows Pow