Book description
Do you know how to use Windows PowerShell to navigate the filesystem and manage files and folders? Or how to retrieve a web page? This introduction to the PowerShell language and scripting environment provides more than 430 task-oriented recipes to help you solve the most complex and pressing problems, and includes more than 100 tried-and-tested scripts that intermediate to advanced system administrators can copy and use immediately.
You'll find hands-on tutorials on fundamentals, common tasks, and administrative jobs that you can apply whether you're on a 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. With Windows PowerShell Cookbook, you’ll get more done in less time.
- Take a tour of PowerShell’s core features, including the command model, object-based pipeline, and ubiquitous scripting
- Learn PowerShell fundamentals such as the interactive shell and fundamental 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, the filesystem, registry, event logs, processes, and services
Table of contents
- Windows PowerShell Cookbook
- A Note Regarding Supplemental Files
- Foreword
- Foreword to the First Edition
- 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
- Resolve Errors Calling Native Executables
- Run a PowerShell Command
- Invoke a Long-Running or Background Command
- Notify Yourself of Job Completion
- Customize Your Shell, Profile, and Prompt
- Find a Command to Accomplish a Task
- Get Help on a Command
- Program: Search Help for Text
- Program: View PowerShell’s HTML Help
- Launch PowerShell at a Specific Location
- Invoke a PowerShell Command or Script from Outside PowerShell
- Customize the Shell to Improve Your Productivity
- Program: Learn Aliases for Common Commands
- Program: Learn Aliases for Common Parameters
- Access and Manage Your Console History
- Program: Create Scripts from Session History
- Invoke a Command from Your Session History
- Program: Search Formatted Output for a Pattern
- Interactively View and Process Command Output
- 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
- Program: 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
- 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 Upper/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
- 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
-
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 UI Frameworks and STA 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
- 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
-
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
- 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
- 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 Data
- Modify the Properties of a WMI Instance
- Invoke a Method on a WMI Class
- Program: Determine Properties Available to WMI 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
- Program: Invoke a PowerShell Expression on a Remote Machine
- Test Connectivity Between Two Computers
- Limit Networking Scripts to Hosts That Respond
- Enable PowerShell Remoting on a Computer
- Enable Remote Desktop on a Computer
- Program: Remotely Enable PowerShell Remoting
- Configure User Permissions for Remoting
- Enable Remoting to Workgroup Computers
- Interactively Manage a Remote Computer
- Invoke a Command on a Remote Computer
- Implicitly Invoke Commands from a Remote Computer
- Create Sessions with Full Network Access
- Pass Variables to Remote Sessions
- Configure Advanced Remoting 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
- Program: Create a Task-Specific Remoting Endpoint
- 30. Transactions
- 31. Event Handling
-
20. Files and Directories
-
V. References
-
A. PowerShell Language and Environment
- Commands and Expressions
- 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, 2nd Edition
- Author(s):
- Release date: August 2010
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596801502
You might also like
book
Deciphering Data Architectures
Data fabric, data lakehouse, and data mesh have recently appeared as viable alternatives to the modern …
book
Fluent React
When it comes to building user interfaces on the web, React enables web developers to unlock …
book
Amazon Web Services in Action, Third Edition
Master essential best practices for deploying and managing applications on Amazon Web Services. This revised bestseller …
book
Generative Deep Learning, 2nd Edition
Generative AI is the hottest topic in tech. This practical book teaches machine learning engineers and …