Microsoft Windows Powershell Step by Step Ebook
Total Page:16
File Type:pdf, Size:1020Kb
PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2007 by Ed Wilson All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2007924649 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 QWT 2 1 0 9 8 7 Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further infor- mation about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to [email protected]. Microsoft, Microsoft Press, Active Accessibility, Active Desktop, Active Directory, ActiveMovie, ActiveStore, ActiveSync, ActiveX, Age of Mythology, Authenticode, BackOffice, BizTalk, ClearType, DataTips, Developer Studio, Direct3D, DirectAnimation, DirectDraw, DirectInput, DirectMusic, DirectPlay, DirectShow, DirectSound, DirectX, Excel, FrontPage, IntelliMouse, IntelliSense, Internet Explorer, J/Direct, Jscript, Liquid Motion, MSDN, MS-DOS, Outlook, PivotChart, PivotTable, PowerPoint, Rushmore, SQL Server, Visual Basic, Visual C++, Visual FoxPro, Visual InterDev, Visual J++, Visual SourceSafe, Visual Studio, Win32, Win32s, Windows, Windows NT, Windows PowerShell, Windows Server, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail 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 without any 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 directly or indirectly by this book. Acquisitions Editor: Martin DelRe Developmental and Project Editor: Maureen Zimmerman Editorial Production: P.M. Gordon Associates Technical Reviewer: Bob Hogan; technical review services provided by Content Master, a member of CM Group, Ltd. Body Part No. X13-68391 To Teresa: my friend, my helper, and my muse. iii Contents at a Glance 1 Overview of Windows PowerShell . .1 2 Using Windows PowerShell Cmdlets . 21 3 Leveraging PowerShell Providers . 45 4 Using PowerShell Scripts . 73 5 Using WMI . 97 6 Querying WMI. 119 7 Working with Active Directory . 145 8 Leveraging the Power of ADO. 175 9 Managing Exchange 2007 . 199 APPENDIX A Cmdlets Installed with Windows PowerShell . 217 APPENDIX B Cmdlet Naming . 221 APPENDIX C Translating VBScript to Windows PowerShell . 223 Index . 289 v Table of Contents Acknowledgments . xi About This Book . xiii 1 Overview of Windows PowerShell . .1 Understanding Windows PowerShell. 1 Using Cmdlets . 3 Installing Windows PowerShell . 3 Deploying Windows PowerShell . 4 Using Command Line Utilities. 5 Security Issues with Windows PowerShell . 7 Controlling Execution of PowerShell Cmdlets. 7 Confirming Commands . 8 Suspending Confirmation of Cmdlets . 10 Working with Windows PowerShell . 11 Accessing Windows PowerShell . 11 Configuring Windows PowerShell . 12 Supplying Options for Cmdlets. 13 Working with the Help Options . 14 Exploring Commands: Step-by-Step Exercises . 16 One Step Further: Obtaining Help . 18 2 Using Windows PowerShell Cmdlets . 21 Understanding the Basics of Cmdlets . 21 Using the Get-ChildItem Cmdlet . 22 Using the Format-Wide Cmdlet . 24 Leveraging the Power of Get-Command . 27 Using the Get-Member Cmdlet . 31 Using the New-Object Cmdlet . 36 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: www.microsoft.com/learning/booksurvey/ vii viii Table of Contents Creating a PowerShell Profile . 38 Working with Cmdlets: Step-by-Step Exercises . 40 One Step Further: Working with New-Object . 42 3 Leveraging PowerShell Providers . 45 Identifying the Providers . 45 Understanding the Alias Provider . 46 Understanding the Certificate Provider . 48 Understanding the Environment Provider . 53 Understanding the File System Provider. 56 Understanding the Function Provider. 60 Understanding the Registry Provider . 62 Understanding the Variable Provider . 64 Exploring the Certificate Provider: Step by Step Exercises. 67 One Step Further: Examining the Environment Provider . 68 4 Using PowerShell Scripts . 73 Why Write Windows PowerShell Scripts . 73 Enabling Script Support. 75 Running Windows PowerShell Scripts . 77 Understanding Variables and Constants . 80 Use of Variables. 80 Use of Constants . 84 Looping Through Code . 85 Using the For Each-Object Cmdlet. 86 Using the For Statement . 88 Using Do … While . 89 Using Do … Until . 90 Making Decisions . 92 Using If … Elseif … Else . 92 Using Switch. 93 Creating Multiple Folders: Step-by-Step Exercises . 94 One Step Further: Deleting Multiple Folders . ..