Windows PowerShell Cookbook

[Pages:586] Windows PowerShell CookbookTM

Other Microsoft .NET resources from O'Reilly

Related titles

Windows PowerShell Quick Reference

Windows Server 2008: The Definitive Guide

Windows Vista Administration

Windows Vista: The Definitive Guide

.NET Books dotnet. is a complete catalog of O'Reilly's books on Resource Center .NET and related technologies, including sample chapters and

code examples.

provides independent coverage of fundamental, interoperable, and emerging Microsoft .NET programming and web services technologies.

Conferences

O'Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the innovator's knowledge into useful skills for those in the trenches. Visit conferences. for our upcoming events.

Safari Bookshelf (safari.) is the premier online reference library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or simply flip to the page you need. Try it today for free.

Windows PowerShell CookbookTM

Lee Holmes

Beijing ? Cambridge ? Farnham ? K?ln ? Paris ? Sebastopol ? Taipei ? Tokyo

Windows PowerShell CookbookTM by Lee Holmes

Copyright ? 2008 Lee Holmes. All rights reserved. Printed in the United States of America.

Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@.

Editor: John Osborn Production Editor: Laurel R.T. Ruma Production Services: Tolman Creek Design

Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read

Printing History:

October 2007:

First Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. Windows PowerShell Cookbook, the image of a box turtle, and related trade dress are trademarks of O'Reilly Media, Inc.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.

While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

This book uses RepKoverTM, a durable and flexible lay-flat binding.

ISBN-10: 0-59652-849-3 ISBN-13: 978-0-596-652849-2 [M]

Table of Contents

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

Part I. Tour

A Guided Tour of Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Introduction

3

An Interactive Shell

4

Structured Commands (Cmdlets)

6

Deep Integration of Objects

7

Administrators As First-Class Users

8

Composable Commands

9

Techniques to Protect You from Yourself

9

Common Discovery Commands

10

Ubiquitous Scripting

11

Ad Hoc Development

12

Bridging Technologies

12

Namespace Navigation Through Providers

14

Much, Much More

16

Part II. Fundamentals

1. The Windows PowerShell Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1.0 Introduction

19

1.1 Run Programs, Scripts, and Existing Tools

19

1.2 Run a PowerShell Command

21

v

1.3 Customize Your Shell, Profile, and Prompt

22

1.4 Find a Command to Accomplish a Task

25

1.5 Get Help on a Command

26

1.6 Program: Search Help for Text

28

1.7 Invoke a PowerShell Script From Outside PowerShell

29

1.8 Program: Retain Changes to Environment Variables Set by a Batch File30

1.9 Get the System Date and Time

32

1.10 Determine the Status of the Last Command

33

1.11 Measure the Duration of a Command

34

1.12 Customize the Shell to Improve Your Productivity

35

1.13 Program: Learn Aliases for Common Commands

36

1.14 Access and Manage Your Console History

38

1.15 Store the Output of a Command into a File

39

1.16 Add Information to the End of a File

41

1.17 Record a Transcript of Your Shell Session

41

1.18 Display the Properties of an Item As a List

42

1.19 Display the Properties of an Item As a Table

42

1.20 Manage the Error Output of Commands

44

1.21 Configure Debug, Verbose, and Progress Output

45

1.22 Extend Your Shell with Additional Snapins

47

1.23 Use Console Files to Load and Save Sets of Snapins

48

2. Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

2.0 Introduction

49

2.1 Filter Items in a List or Command Output

50

2.2 Program: Simplify Most Where-Object Filters

51

2.3 Program: Interactively Filter Lists of Objects

52

2.4 Work with Each Item in a List or Command Output

54

2.5 Automate Data-Intensive Tasks

56

3. Variables and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.0 Introduction

61

3.1 Store Information in Variables

62

3.2 Access Environment Variables

63

3.3 Control Access and Scope of Variables and Other Items

65

3.4 Work with .NET Objects

67

3.5 Create an Instance of a .NET Object

71

3.6 Program: Create Instances of Generic Objects

73

3.7 Reduce Typing for Long Class Names

74

vi | Table of Contents

3.8 Use a COM Object

76

3.9 Learn About Types and Objects

77

3.10 Get Detailed Documentation About Types and Objects

78

3.11 Add Custom Methods and Properties to Objects

80

3.12 Add Custom Methods and Properties to Types

82

4. Looping and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

4.0 Introduction

87

4.1 Make Decisions with Comparison and Logical Operators

87

4.2 Adjust Script Flow Using Conditional Statements

89

4.3 Manage Large Conditional Statements with Switches

90

4.4 Repeat Operations with Loops

91

4.5 Add a Pause or Delay

93

5. Strings and Unstructured Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

5.0 Introduction

95

5.1 Create a String

95

5.2 Create a Multiline or Formatted String

97

5.3 Place Special Characters in a String

98

5.4 Insert Dynamic Information in a String

99

5.5 Prevent a String from Including Dynamic Information

100

5.6 Place Formatted Information in a String

101

5.7 Search a String for Text or a Pattern

102

5.8 Replace Text in a String

105

5.9 Convert a String to Upper/Lowercase

106

5.10 Trim a String

107

5.11 Format a Date for Output

108

5.12 Program: Convert Text Streams to Objects

110

5.13 Generate Large Reports and Text Streams

114

6. Calculations and Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

6.0 Introduction

117

6.1 Perform Simple Arithmetic

117

6.2 Perform Complex Arithmetic

119

6.3 Measure Statistical Properties of a List

121

6.4 Work with Numbers As Binary

123

6.5 Simplify Math with Administrative Constants

127

6.6 Convert Numbers Between Bases

128

Table of Contents | vii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download