Book description
This Cookbook by Windows PowerShell team developer Lee Holmes provides hundreds of tested scripts that you can use right away to get Microsoft's new tool working for you. More than 150 recipes, combined with a concise task-based introduction to the Windows PowerShell scripting language and environment, make it the perfect look-up guide when you encounter a thorny problem, or need a quick solution. The ideal companion to any tutorial or reference, this book meets the needs of system administrators at any level.
Microsoft has revolutionized the world of system management and command-line shells with its release of Windows PowerShell, and Lee Holmes gives you practical tools and inside advice that will make you a more productive user and administrator. You will be able to solve everything from automating routine tasks, working with files, event logs and other forms of structured data, to managing the users and resources of complex Windows networks. Each recipe includes a focused piece of code plus discussion of how and why it works, so that you can apply the solution to similar tasks.
You get an array of recipes covering PowerShell fundamentals, common tasks, and administrator tasks, including:
- Pipelines, variables, objects, looping and flow control, strings and unstructured text, calculations and math
- Simple files; structured files; Internet-enabled scripts; code reuse; lists, arrays and hashtables; user feedback; error management; environmental awareness; script signing; and more
- Files and directories, registry manipulation, comparing data, event logs, process cmdlets, service cmdlets, Active Directory, enterprise computer management, and more
With working scripts, tutorials, and references all in one volume, Windows PowerShell Cookbook will turbocharge the productivity of any Windows administrator.
Publisher resources
Table of contents
- A Note Regarding Supplemental Files
- Foreword
- Preface
-
I. Tour
-
A Guided Tour of Windows PowerShell
- Introduction
- An Interactive Shell
- Structured Commands (Cmdlets)
- Deep Integration of Objects
- Administrators As First-Class Users
- Composable Commands
- Techniques to Protect You from Yourself
- Common Discovery Commands
- Ubiquitous Scripting
- Ad Hoc Development
- Bridging Technologies
- Namespace Navigation Through Providers
- Much, Much More
-
A Guided Tour of Windows PowerShell
-
II. Fundamentals
-
1. The Windows PowerShell Interactive Shell
- Introduction
- 1.1. Run Programs, Scripts, and Existing Tools
- 1.2. Run a PowerShell Command
- 1.3. Customize Your Shell, Profile, and Prompt
- 1.4. Find a Command to Accomplish a Task
- 1.5. Get Help on a Command
- 1.6. Program: Search Help for Text
- 1.7. Invoke a PowerShell Script From Outside PowerShell
- 1.8. Program: Retain Changes to Environment Variables Set by a Batch File
- 1.9. Get the System Date and Time
- 1.10. Determine the Status of the Last Command
- 1.11. Measure the Duration of a Command
- 1.12. Customize the Shell to Improve Your Productivity
- 1.13. Program: Learn Aliases for Common Commands
- 1.14. Access and Manage Your Console History
- 1.15. Store the Output of a Command into a File
- 1.16. Add Information to the End of a File
- 1.17. Record a Transcript of Your Shell Session
- 1.18. Display the Properties of an Item As a List
- 1.19. Display the Properties of an Item As a Table
- 1.20. Manage the Error Output of Commands
- 1.21. Configure Debug, Verbose, and Progress Output
- 1.22. Extend Your Shell with Additional Snapins
- 1.23. Use Console Files to Load and Save Sets of Snapins
- 2. Pipelines
-
3. Variables and Objects
- 3.0. Introduction
- 3.1. Store Information in Variables
- 3.2. Access Environment Variables
- 3.3. Control Access and Scope of Variables and Other Items
- 3.4. Work with .NET Objects
- 3.5. Create an Instance of a .NET Object
- 3.6. Program: Create Instances of Generic Objects
- 3.7. Reduce Typing for Long Class Names
- 3.8. Use a COM Object
- 3.9. Learn About Types and Objects
- 3.10. Get Detailed Documentation About Types and Objects
- 3.11. Add Custom Methods and Properties to Objects
- 3.12. Add Custom Methods and Properties to Types
- 4. Looping and Flow Control
-
5. Strings and Unstructured Text
- 5.0. Introduction
- 5.1. Create a String
- 5.2. Create a Multiline or Formatted String
- 5.3. Place Special Characters in a String
- 5.4. Insert Dynamic Information in a String
- 5.5. Prevent a String from Including Dynamic Information
- 5.6. Place Formatted Information in a String
- 5.7. Search a String for Text or a Pattern
- 5.8. Replace Text in a String
- 5.9. Convert a String to Upper/Lowercase
- 5.10. Trim a String
- 5.11. Format a Date for Output
- 5.12. Program: Convert Text Streams to Objects
- 5.13. Generate Large Reports and Text Streams
- 6. Calculations and Math
-
1. The Windows PowerShell Interactive Shell
-
III. Common Tasks
- 7. Simple Files
-
8. Structured Files
- 8.0. Introduction
- 8.1. Access Information in an XML File
- 8.2. Perform an XPath Query Against an XML File
- 8.3. Modify Data in an XML File
- 8.4. Easily Import and Export Your Structured Data
- 8.5. Store the Output of a Command in a CSV File
- 8.6. Import Structured Data from a CSV File
- 8.7. Use Excel to Manage Command Output
- 9. Internet-Enabled Scripts
-
10. Code Reuse
- 10.0. Introduction
- 10.1. Write a Script
- 10.2. Write a Function
- 10.3. Write a Script Block
- 10.4. Return Data from a Script, Function, or Script Block
- 10.5. Place Common Functions in a Library
- 10.6. Access Arguments of a Script, Function, or Script Block
- 10.7. Access Pipeline Input
- 10.8. Write Pipeline-Oriented Scripts with Cmdlet Keywords
- 10.9. Write a Pipeline-Oriented Function
-
11. Lists, Arrays, and Hashtables
- 11.0. Introduction
- 11.1. Create an Array or List of Items
- 11.2. Create a Jagged or Multidimensional Array
- 11.3. Access Elements of an Array
- 11.4. Visit Each Element of an Array
- 11.5. Sort an Array or List of Items
- 11.6. Determine Whether an Array Contains an Item
- 11.7. Combine Two Arrays
- 11.8. Find Items in an Array That Match a Value
- 11.9. Remove Elements from an Array
- 11.10. Find Items in an Array Greater or Less Than a Value
- 11.11. Use the ArrayList Class for Advanced Array Tasks
- 11.12. Create a Hashtable or Associative Array
- 11.13. Sort a Hashtable by Key or Value
-
12. User Interaction
- 12.0. Introduction
- 12.1. Read a Line of User Input
- 12.2. Read a Key of User Input
- 12.3. Program: Display a Menu to the User
- 12.4. Display Messages and Output to the User
- 12.5. Provide Progress Updates on Long-Running Tasks
- 12.6. Write Culture-Aware Scripts
- 12.7. Program: Invoke a Script Block with Alternate Culture Settings
- 12.8. Access Features of the Host’s User Interface
- 12.9. Program: Add a Graphical User Interface to Your Script
- 13. Tracing and Error Management
-
14. Environmental Awareness
- 14.0. Introduction
- 14.1. View and Modify Environment Variables
- 14.2. Access Information About Your Command’s Invocation
- 14.3. Program: Investigate the InvocationInfo Variable
- 14.4. Find Your Script’s Name
- 14.5. Find Your Script’s Location
- 14.6. Find the Location of Common System Paths
- 14.7. Program: Search the Windows Start Menu
- 14.8. Get the Current Location
- 14.9. Safely Build File Paths Out of Their Components
- 14.10. Interact with PowerShell’s Global Environment
-
15. Extend the Reach of Windows PowerShell
- 15.0. Introduction
- 15.1. Access Windows Management Instrumentation Data
- 15.2. Program: Determine Properties Available to WMI Filters
- 15.3. Program: Search for WMI Classes
- 15.4. Use .NET to Perform Advanced WMI Tasks
- 15.5. Convert a VBScript WMI Script to PowerShell
- 15.6. Automate Programs Using COM Scripting Interfaces
- 15.7. Program: Query a SQL Data Source
- 15.8. Access Windows Performance Counters
- 15.9. Program: Invoke Native Windows API Calls
- 15.10. Program: Add Inline C# to Your PowerShell Script
- 15.11. Access a .NET SDK Library
- 15.12. Create Your Own PowerShell Cmdlet
- 15.13. Add PowerShell Scripting to Your Own Program
-
16. Security and Script Signing
- 16.0. Introduction
- 16.1. Enable Scripting Through an Execution Policy
- 16.2. Sign a PowerShell Script or Formatting File
- 16.3. Program: Create a Self-Signed Certificate
- 16.4. Manage PowerShell Security in an Enterprise
- 16.5. Verify the Digital Signature of a PowerShell Script
- 16.6. Securely Handle Sensitive Information
- 16.7. Securely Request Usernames and Passwords
- 16.8. Program: Start a Process As Another User
- 16.9. Securely Store Credentials on Disk
- 16.10. Access User and Machine Certificates
- 16.11. Program: Search the Certificate Store
-
IV. Administrator Tasks
-
17. Files and Directories
- 17.0. Introduction
- 17.1. Find All Files Modified Before a Certain Date
- 17.2. Clear or Remove a File
- 17.3. Manage and Change the Attributes of a File
- 17.4. Get the Files in a Directory
- 17.5. Find Files That Match a Pattern
- 17.6. Manage Files That Include Special Characters
- 17.7. Program: Get Disk Usage Information
- 17.8. Determine the Current Location
- 17.9. Monitor a File for Changes
- 17.10. Program: Get the MD5 or SHA1 Hash of a File
- 17.11. Create a Directory
- 17.12. Remove a File or Directory
- 17.13. Rename a File or Directory
- 17.14. Move a File or Directory
- 17.15. Get the ACL of a File or Directory
- 17.16. Set the ACL of a File or Directory
- 17.17. Program: Add Extended File Properties to Files
- 17.18. Program: Create a Filesystem Hard Link
- 17.19. Program: Create a ZIP Archive
-
18. The Windows Registry
- 18.0. Introduction
- 18.1. Navigate the Registry
- 18.2. View a Registry Key
- 18.3. Modify or Remove a Registry Key Value
- 18.4. Create a Registry Key Value
- 18.5. Remove a Registry Key
- 18.6. Add a Site to an Internet Explorer Security Zone
- 18.7. Modify Internet Explorer Settings
- 18.8. Program: Search the Windows Registry
- 18.9. Get the ACL of a Registry Key
- 18.10. Set the ACL of a Registry Key
- 18.11. Work with the Registry of a Remote Computer
- 18.12. Program: Get Registry Items from Remote Machines
- 18.13. Program: Get Properties of Remote Registry Keys
- 18.14. Program: Set Properties of Remote Registry Keys
- 18.15. Discover Registry Settings for Programs
- 19. Comparing Data
-
20. Event Logs
- 20.0. Introduction
- 20.1. List All Event Logs
- 20.2. Get the Newest Entries from an Event Log
- 20.3. Find Event Log Entries with Specific Text
- 20.4. Retrieve a Specific Event Log Entry
- 20.5. Find Event Log Entries by Their Frequency
- 20.6. Back Up an Event Log
- 20.7. Create or Remove an Event Log
- 20.8. Write to an Event Log
- 20.9. Access Event Logs of a Remote Machine
- 21. Processes
- 22. System Services
-
23. Active Directory
- 23.0. Introduction
- 23.1. Test Active Directory Scripts on a Local Installation
- 23.2. Create an Organizational Unit
- 23.3. Get the Properties of an Organizational Unit
- 23.4. Modify Properties of an Organizational Unit
- 23.5. Get the Children of an Active Directory Container
- 23.6. Create a User Account
- 23.7. Search for a User Account
- 23.8. Get and List the Properties of a User Account
- 23.9. Modify Properties of a User Account
- 23.10. Create a Security or Distribution Group
- 23.11. Search for a Security or Distribution Group
- 23.12. Get the Properties of a Group
- 23.13. Find the Owner of a Group
- 23.14. Modify Properties of a Security or Distribution Group
- 23.15. Add a User to a Security or Distribution Group
- 23.16. Remove a User from a Security or Distribution Group
- 23.17. List a User’s Group Membership
- 23.18. List the Members of a Group
- 23.19. List the Users in an Organizational Unit
- 23.20. Search for a Computer Account
- 23.21. Get and List the Properties of a Computer Account
-
24. Enterprise Computer Management
- 24.0. Introduction
- 24.1. Program: List Logon or Logoff Scripts for a User
- 24.2. Program: List Startup or Shutdown Scripts for a Machine
- 24.3. Enable or Disable the Windows Firewall
- 24.4. Open or Close Ports in the Windows Firewall
- 24.5. Program: List All Installed Software
- 24.6. Uninstall an Application
- 24.7. Manage Scheduled Tasks on a Computer
- 24.8. Retrieve Printer Information
- 24.9. Retrieve Printer Queue Statistics
- 24.10. Manage Printers and Print Queues
- 24.11. Determine Whether a Hotfix Is Installed
- 24.12. Program: Summarize System Information
- 24.13. Renew a DHCP Lease
- 24.14. Assign a Static IP Address
- 24.15. List All IP Addresses for a Computer
- 24.16. List Network Adapter Properties
- 25. Manage an Exchange 2007 Server
- 26. Manage an Operations Manager 2007 Server
-
17. Files and Directories
-
V. References
-
A. PowerShell Language and Environment
- A.1. Commands and Expressions
- A.2. Comments
- A.3. Variables
- A.4. Booleans
- A.5. Strings
- A.6. Numbers
- A.7. Arrays and Lists
- A.8. Hashtables (Associative Arrays)
- A.9. XML
- A.10. Simple Operators
- A.11. Comparison Operators
- A.12. Conditional Statements
- A.13. Looping Statements
- A.14. Working with the .NET Framework
- A.15. Writing Scripts, Reusing Functionality
- A.16. Managing Errors
- A.17. Formatting Output
- A.18. Capturing Output
- A.19. Tracing and Debugging
- A.20. Common Customization Points
- B. Regular Expression Reference
- C. PowerShell Automatic Variables
- D. Standard PowerShell Verbs
- E. Selected .NET Classes and Their Uses
- F. WMI Reference
- G. Selected COM Objects and Their Uses
- H. .NET String Formatting
- I. .NET DateTime Formatting
-
A. PowerShell Language and Environment
- Index
- About the Author
- Colophon
- Copyright
Product information
- Title: Windows PowerShell Cookbook
- Author(s):
- Release date: October 2007
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596528492
You might also like
book
Windows PowerShell Cookbook, 2nd Edition
Do you know how to use Windows PowerShell to navigate the filesystem and manage files and …
book
Windows PowerShell Best Practices
Expert recommendations, pragmatically applied. Automate system administration using Windows PowerShell best practices—and optimize your operational efficiency. …
book
Windows PowerShell for Developers
Want to perform programming tasks better, faster, simpler, and make them repeatable? Take a deep dive …
book
Windows PowerShell Unleashed
A new edition of this title is available, ISBN-10: 0672329883 ISBN-13: 9780672329883 PowerShell is one of …