Windows PowerShell Cookbook - LinuxCLG

[Pages:884] Windows PowerShell Cookbook

SECOND EDITION

Windows PowerShell Cookbook

Lee Holmes

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

Windows PowerShell Cookbook, Second Edition

by Lee Holmes

Copyright ? 2010 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 (). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@.

Editor: Mike Hendrickson Production Editor: Teresa Elsey Copyeditor: Genevieve d'Entremont Proofreader: Teresa Elsey

Indexer: Newgen North America, Inc. Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano

Printing History:

October 2007:

August 2010:

First Edition. Second 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 authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

ISBN: 978-0-596-80150-2 [M] 1281554603

Table of Contents

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Foreword to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

Part I. Tour

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

Part II. Fundamentals

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

1.1 Run Programs, Scripts, and Existing Tools

19

1.2 Resolve Errors Calling Native Executables

21

1.3 Run a PowerShell Command

23

1.4 Invoke a Long-Running or Background Command

24

1.5 Notify Yourself of Job Completion

27

1.6 Customize Your Shell, Profile, and Prompt

28

1.7 Find a Command to Accomplish a Task

31

1.8 Get Help on a Command

32

1.9 Program: Search Help for Text

34

1.10 Program: View PowerShell's HTML Help

36

1.11 Launch PowerShell at a Specific Location

37

1.12 Invoke a PowerShell Command or Script from Outside PowerShell 38

1.13 Customize the Shell to Improve Your Productivity

40

1.14 Program: Learn Aliases for Common Commands

42

1.15 Program: Learn Aliases for Common Parameters

44

1.16 Access and Manage Your Console History

46

1.17 Program: Create Scripts from Session History

48

v

1.18 Invoke a Command from Your Session History

49

1.19 Program: Search Formatted Output for a Pattern

51

1.20 Interactively View and Process Command Output

52

1.21 Store the Output of a Command into a File

54

1.22 Add Information to the End of a File

55

1.23 Record a Transcript of Your Shell Session

55

1.24 Extend Your Shell with Additional Commands

56

1.25 Use Commands from Customized Shells

57

1.26 Save State Between Sessions

59

2. Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

2.1 Filter Items in a List or Command Output

64

2.2 Group and Pivot Data by Name

65

2.3 Program: Simplify Most Where-Object Filters

68

2.4 Program: Interactively Filter Lists of Objects

70

2.5 Work with Each Item in a List or Command Output

72

2.6 Automate Data-Intensive Tasks

74

2.7 Program: Simplify Most Foreach-Object Pipelines

78

2.8 Intercept Stages of the Pipeline

80

2.9 Automatically Capture Pipeline Output

81

2.10 Capture and Redirect Binary Process Output

83

3. Variables and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

3.1 Display the Properties of an Item as a List

90

3.2 Display the Properties of an Item as a Table

92

3.3 Store Information in Variables

94

3.4 Access Environment Variables

95

3.5 Program: Retain Changes to Environment Variables Set by a Batch

File

98

3.6 Control Access and Scope of Variables and Other Items

100

3.7 Program: Create a Dynamic Variable

102

3.8 Work with .NET Objects

104

3.9 Create an Instance of a .NET Object

108

3.10 Program: Create Instances of Generic Objects

110

3.11 Reduce Typing for Long Class Names

113

3.12 Use a COM Object

115

3.13 Learn About Types and Objects

115

3.14 Get Detailed Documentation About Types and Objects

117

3.15 Add Custom Methods and Properties to Objects

119

3.16 Create and Initialize Custom Objects

121

3.17 Add Custom Methods and Properties to Types

125

vi | Table of Contents

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

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

Google Online Preview   Download