Book description
How do you use Windows PowerShell to navigate the filesystem, manage files and folders, or retrieve a web page? This introduction to the PowerShell language and scripting environment provides more than 400 task-oriented recipes to help you solve all kinds of problems. Intermediate to advanced system administrators will find more than 100 tried-and-tested scripts they can copy and use immediately.
Updated for PowerShell 3.0, this comprehensive cookbook includes hands-on recipes for common tasks and administrative jobs that you can apply whether you’re on the client or server version of Windows. You also get quick references to technologies used in conjunction with PowerShell, including format specifiers and frequently referenced registry keys to selected .NET, COM, and WMI classes.
- Learn how to use PowerShell on Windows 8 and Windows Server 2012
- Tour PowerShell’s core features, including the command model, object-based pipeline, and ubiquitous scripting
- Master fundamentals such as the interactive shell, pipeline, and object concepts
- Perform common tasks that involve working with files, Internet-connected scripts, user interaction, and more
- Solve tasks in systems and enterprise management, such as working with Active Directory and the filesystem
Publisher resources
Table of contents
- Windows PowerShell Cookbook
- 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
- Run Programs, Scripts, and Existing Tools
- Run a PowerShell Command
- Resolve Errors Calling Native Executables
- Supply Default Values for Parameters
- Invoke a Long-Running or Background Command
- Program: Monitor a Command for Changes
- Notify Yourself of Job Completion
- Customize Your Shell, Profile, and Prompt
- Customize PowerShell’s User Input Behavior
- Customize PowerShell’s Command Resolution Behavior
- Find a Command to Accomplish a Task
- Get Help on a Command
- Update System Help Content
- Program: Search Help for Text
- Launch PowerShell at a Specific Location
- Invoke a PowerShell Command or Script from Outside PowerShell
- Understand and Customize PowerShell’s Tab Completion
- Program: Learn Aliases for Common Commands
- Program: Learn Aliases for Common Parameters
- Access and Manage Your Console History
- Program: Create Scripts from Your Session History
- Invoke a Command from Your Session History
- Program: Search Formatted Output for a Pattern
- Interactively View and Process Command Output
- Program: Interactively View and Explore Objects
- Store the Output of a Command into a File
- Add Information to the End of a File
- Record a Transcript of Your Shell Session
- Extend Your Shell with Additional Commands
- Use Commands from Customized Shells
- Save State Between Sessions
-
2. Pipelines
- Introduction
- Filter Items in a List or Command Output
- Group and Pivot Data by Name
- Program: Simplify Most Where-Object Filters
- Program: Interactively Filter Lists of Objects
- Work with Each Item in a List or Command Output
- Automate Data-Intensive Tasks
- Program: Simplify Most Foreach-Object Pipelines
- Intercept Stages of the Pipeline
- Automatically Capture Pipeline Output
- Capture and Redirect Binary Process Output
-
3. Variables and Objects
- Introduction
- Display the Properties of an Item as a List
- Display the Properties of an Item as a Table
- Store Information in Variables
- Access Environment Variables
- Program: Retain Changes to Environment Variables Set by a Batch File
- Control Access and Scope of Variables and Other Items
- Program: Create a Dynamic Variable
- Work with .NET Objects
- Create an Instance of a .NET Object
- Create Instances of Generic Objects
- Reduce Typing for Long Class Names
- Use a COM Object
- Learn About Types and Objects
- Get Detailed Documentation About Types and Objects
- Add Custom Methods and Properties to Objects
- Create and Initialize Custom Objects
- Add Custom Methods and Properties to Types
- Define Custom Formatting for a Type
- 4. Looping and Flow Control
-
5. Strings and Unstructured Text
- Introduction
- Create a String
- Create a Multiline or Formatted String
- Place Special Characters in a String
- Insert Dynamic Information in a String
- Prevent a String from Including Dynamic Information
- Place Formatted Information in a String
- Search a String for Text or a Pattern
- Replace Text in a String
- Split a String on Text or a Pattern
- Combine Strings into a Larger String
- Convert a String to Uppercase or Lowercase
- Trim a String
- Format a Date for Output
- Program: Convert Text Streams to Objects
- Generate Large Reports and Text Streams
- Generate Source Code and Other Repetitive Text
- 6. Calculations and Math
-
7. Lists, Arrays, and Hashtables
- Introduction
- Create an Array or List of Items
- Create a Jagged or Multidimensional Array
- Access Elements of an Array
- Visit Each Element of an Array
- Sort an Array or List of Items
- Determine Whether an Array Contains an Item
- Combine Two Arrays
- Find Items in an Array That Match a Value
- Compare Two Lists
- Remove Elements from an Array
- Find Items in an Array Greater or Less Than a Value
- Use the ArrayList Class for Advanced Array Tasks
- Create a Hashtable or Associative Array
- Sort a Hashtable by Key or Value
- 8. Utility Tasks
-
1. The Windows PowerShell Interactive Shell
-
III. Common Tasks
- 9. Simple Files
-
10. Structured Files
- Introduction
- Access Information in an XML File
- Perform an XPath Query Against XML
- Convert Objects to XML
- Modify Data in an XML File
- Easily Import and Export Your Structured Data
- Store the Output of a Command in a CSV or Delimited File
- Import CSV and Delimited Data from a File
- Manage JSON Data Streams
- Use Excel to Manage Command Output
- Parse and Interpret PowerShell Scripts
-
11. Code Reuse
- Introduction
- Write a Script
- Write a Function
- Find a Verb Appropriate for a Command Name
- Write a Script Block
- Return Data from a Script, Function, or Script Block
- Package Common Commands in a Module
- Write Commands That Maintain State
- Selectively Export Commands from a Module
- Diagnose and Interact with Internal Module State
- Handle Cleanup Tasks When a Module Is Removed
- Access Arguments of a Script, Function, or Script Block
- Add Validation to Parameters
- Accept Script Block Parameters with Local Variables
- Dynamically Compose Command Parameters
- Provide -WhatIf, -Confirm, and Other Cmdlet Features
- Add Help to Scripts or Functions
- Add Custom Tags to a Function or Script Block
- Access Pipeline Input
- Write Pipeline-Oriented Scripts with Cmdlet Keywords
- Write a Pipeline-Oriented Function
- Organize Scripts for Improved Readability
- Invoke Dynamically Named Commands
- Program: Enhance or Extend an Existing Cmdlet
-
12. Internet-Enabled Scripts
- Introduction
- Download a File from an FTP or Internet Site
- Upload a File to an FTP Site
- Download a Web Page from the Internet
- Parse and Analyze a Web Page from the Internet
- Script a Web Application Session
- Program: Get-PageUrls
- Interact with REST-Based Web APIs
- Connect to a Web Service
- Export Command Output as a Web Page
- Send an Email
- Program: Monitor Website Uptimes
- Program: Interact with Internet Protocols
-
13. User Interaction
- Introduction
- Read a Line of User Input
- Read a Key of User Input
- Program: Display a Menu to the User
- Display Messages and Output to the User
- Provide Progress Updates on Long-Running Tasks
- Write Culture-Aware Scripts
- Support Other Languages in Script Output
- Program: Invoke a Script Block with Alternate Culture Settings
- Access Features of the Host’s User Interface
- Program: Add a Graphical User Interface to Your Script
- Interact with MTA Objects
- 14. Debugging
-
15. Tracing and Error Management
- Introduction
- Determine the Status of the Last Command
- View the Errors Generated by a Command
- Manage the Error Output of Commands
- Program: Resolve an Error
- Configure Debug, Verbose, and Progress Output
- Handle Warnings, Errors, and Terminating Errors
- Output Warnings, Errors, and Terminating Errors
- Program: Analyze a Script’s Performance Profile
-
16. Environmental Awareness
- Introduction
- View and Modify Environment Variables
- Modify the User or System Path
- Access Information About Your Command’s Invocation
- Program: Investigate the InvocationInfo Variable
- Find Your Script’s Name
- Find Your Script’s Location
- Find the Location of Common System Paths
- Get the Current Location
- Safely Build File Paths Out of Their Components
- Interact with PowerShell’s Global Environment
- Determine PowerShell Version Information
- Test for Administrative Privileges
-
17. Extend the Reach of Windows PowerShell
- Introduction
- Automate Programs Using COM Scripting Interfaces
- Program: Query a SQL Data Source
- Access Windows Performance Counters
- Access Windows API Functions
- Program: Invoke Simple Windows API Calls
- Define or Extend a .NET Class
- Add Inline C# to Your PowerShell Script
- Access a .NET SDK Library
- Create Your Own PowerShell Cmdlet
- Add PowerShell Scripting to Your Own Program
-
18. Security and Script Signing
- Introduction
- Enable Scripting Through an Execution Policy
- Disable Warnings for UNC Paths
- Sign a PowerShell Script, Module, or Formatting File
- Program: Create a Self-Signed Certificate
- Manage PowerShell Security in an Enterprise
- Block Scripts by Publisher, Path, or Hash
- Verify the Digital Signature of a PowerShell Script
- Securely Handle Sensitive Information
- Securely Request Usernames and Passwords
- Program: Start a Process as Another User
- Program: Run a Temporarily Elevated Command
- Securely Store Credentials on Disk
- Access User and Machine Certificates
- Program: Search the Certificate Store
- Add and Remove Certificates
- Manage Security Descriptors in SDDL Form
- 19. Integrated Scripting Environment
-
IV. Administrator Tasks
-
20. Files and Directories
- Introduction
- Determine the Current Location
- Get the Files in a Directory
- Find All Files Modified Before a Certain Date
- Clear the Content of a File
- Manage and Change the Attributes of a File
- Find Files That Match a Pattern
- Manage Files That Include Special Characters
- Program: Get Disk Usage Information
- Monitor a File for Changes
- Get the Version of a DLL or Executable
- Program: Get the MD5 or SHA1 Hash of a File
- Create a Directory
- Remove a File or Directory
- Rename a File or Directory
- Move a File or Directory
- Create and Map PowerShell Drives
- Access Long File and Directory Names
- Unblock a File
- Interact with Alternate Data Streams
- Program: Move or Remove a Locked File
- Get the ACL of a File or Directory
- Set the ACL of a File or Directory
- Program: Add Extended File Properties to Files
- Program: Create a Filesystem Hard Link
- Program: Create a ZIP Archive
-
21. The Windows Registry
- Introduction
- Navigate the Registry
- View a Registry Key
- Modify or Remove a Registry Key Value
- Create a Registry Key Value
- Remove a Registry Key
- Safely Combine Related Registry Modifications
- Add a Site to an Internet Explorer Security Zone
- Modify Internet Explorer Settings
- Program: Search the Windows Registry
- Get the ACL of a Registry Key
- Set the ACL of a Registry Key
- Work with the Registry of a Remote Computer
- Program: Get Registry Items from Remote Machines
- Program: Get Properties of Remote Registry Keys
- Program: Set Properties of Remote Registry Keys
-
Discover Registry Settings for Programs
- Problem
- Solution
-
Discussion
- Launch and configure Process Monitor
- Prepare to manually set the configuration option
- Tell Process Monitor to begin capturing information
- Manually set the configuration option
- Tell Process Monitor to stop capturing information
- Review the capture logs for registry modification
- Automate these registry writes
- See Also
- 22. Comparing Data
-
23. Event Logs
- Introduction
- List All Event Logs
- Get the Newest Entries from an Event Log
- Find Event Log Entries with Specific Text
- Retrieve and Filter Event Log Entries
- Find Event Log Entries by Their Frequency
- Back Up an Event Log
- Create or Remove an Event Log
- Write to an Event Log
- Run a PowerShell Script for Windows Event Log Entries
- Clear or Maintain an Event Log
- Access Event Logs of a Remote Machine
- 24. Processes
- 25. System Services
-
26. Active Directory
- Introduction
- Test Active Directory Scripts on a Local Installation
- Create an Organizational Unit
- Get the Properties of an Organizational Unit
- Modify Properties of an Organizational Unit
- Delete an Organizational Unit
- Get the Children of an Active Directory Container
- Create a User Account
- Program: Import Users in Bulk to Active Directory
- Search for a User Account
- Get and List the Properties of a User Account
- Modify Properties of a User Account
- Change a User Password
- Create a Security or Distribution Group
- Search for a Security or Distribution Group
- Get the Properties of a Group
- Find the Owner of a Group
- Modify Properties of a Security or Distribution Group
- Add a User to a Security or Distribution Group
- Remove a User from a Security or Distribution Group
- List a User’s Group Membership
- List the Members of a Group
- List the Users in an Organizational Unit
- Search for a Computer Account
- Get and List the Properties of a Computer Account
-
27. Enterprise Computer Management
- Introduction
- Join a Computer to a Domain or Workgroup
- Remove a Computer from a Domain
- Rename a Computer
- Program: List Logon or Logoff Scripts for a User
- Program: List Startup or Shutdown Scripts for a Machine
- Deploy PowerShell-Based Logon Scripts
- Enable or Disable the Windows Firewall
- Open or Close Ports in the Windows Firewall
- Program: List All Installed Software
- Uninstall an Application
- Manage Computer Restore Points
- Reboot or Shut Down a Computer
- Determine Whether a Hotfix Is Installed
- Manage Scheduled Tasks on a Computer
- Retrieve Printer Information
- Retrieve Printer Queue Statistics
- Manage Printers and Print Queues
- Program: Summarize System Information
- Renew a DHCP Lease
- Assign a Static IP Address
- List All IP Addresses for a Computer
- List Network Adapter Properties
-
28. Windows Management Instrumentation
- Introduction
- Access Windows Management Instrumentation and CIM Data
- Modify the Properties of a WMI or CIM Instance
- Invoke a Method on a WMI Instance or Class
- Program: Determine Properties Available to WMI and CIM Filters
- Program: Search for WMI Classes
- Use .NET to Perform Advanced WMI Tasks
- Improve the Performance of Large-Scale WMI Operations
- Convert a VBScript WMI Script to PowerShell
-
29. Remoting
- Introduction
- Find Commands That Support Their Own Remoting
- Enable PowerShell Remoting on a Computer
- Interactively Manage a Remote Computer
- Invoke a Command on a Remote Computer
- Disconnect and Reconnect PowerShell Sessions
- Program: Remotely Enable PowerShell Remoting
- Program: Invoke a PowerShell Expression on a Remote Machine
- Test Connectivity Between Two Computers
- Limit Networking Scripts to Hosts That Respond
- Enable Remote Desktop on a Computer
- Configure User Permissions for Remoting
- Enable Remoting to Workgroup Computers
- Implicitly Invoke Commands from a Remote Computer
- Create Sessions with Full Network Access
- Pass Variables to Remote Sessions
- Configure Advanced Remoting Quotas and Options
- Invoke a Command on Many Computers
- Run a Local Script on a Remote Computer
- Program: Transfer a File to a Remote Computer
- Determine Whether a Script Is Running on a Remote Computer
- Create a Task-Specific Remoting Endpoint
-
30. Workflows
- Introduction
- Write a Workflow
- Run a Workflow
- Suspend and Resume a Workflow
- Invoke Islands of Traditional PowerShell Script
- Invoke Workflow Actions in Parallel
- Customize an Activity’s Connection Parameters
- Write a Workflow That Requires Human Intervention
- Add Raw XAML to a Workflow
- Reference Custom Activities in a Workflow
- Debug or Troubleshoot a Workflow
- Use PowerShell Activities from a Traditional Windows Workflow Application
- 31. Transactions
- 32. Event Handling
-
20. Files and Directories
-
V. References
-
A. PowerShell Language and Environment
- Commands and Expressions
- Comments
- Help Comments
- Variables
- Booleans
- Strings
- Numbers
- Arrays and Lists
- Hashtables (Associative Arrays)
- XML
- Simple Operators
- Comparison Operators
- Conditional Statements
- Looping Statements
- Working with the .NET Framework
- Writing Scripts, Reusing Functionality
- Managing Errors
- Formatting Output
- Capturing Output
- Common Customization Points
- B. Regular Expression Reference
- C. XPath Quick Reference
- D. .NET String Formatting
- E. .NET DateTime Formatting
- F. Selected .NET Classes and Their Uses
- G. WMI Reference
- H. Selected COM Objects and Their Uses
- I. Selected Events and Their Uses
- J. Standard PowerShell Verbs
-
A. PowerShell Language and Environment
- Index
- About the Author
- Colophon
- Copyright
Product information
- Title: Windows PowerShell Cookbook, 3rd Edition
- Author(s):
- Release date: January 2013
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781449320683
You might also like
book
Windows PowerShell in Action, Third Edition
Windows PowerShell in Action, Third Edition is an end-to-end revision of the definitive guide to PowerShell. …
book
Windows Server Automation with PowerShell Cookbook - Fourth Edition
Develop a holistic understanding of Windows Server with over 100 PowerShell recipes Key Features Updated with …
book
Windows Server 2019 Automation with PowerShell Cookbook - Third Edition
Automate Windows server tasks with the powerful features of the PowerShell Language Key Features Leverage PowerShell …
book
PowerShell in Depth, Second Edition
PowerShell in Depth, Second Edition is the go-to reference for administrators working with Windows PowerShell. Every …