Module 4 Lecture Notes
Module Overview
PowerShell Programming Module 4: Using the PowerShell Pipeline
The pipeline allows commands to be connected and enables the output of one command to be passed as input to another.
The pipeline character is represented by the vertical bar (|).
Understanding the Pipeline
Concept: Similar to other shell languages, where commands can be piped together.
Example in Cmd.exe:
PS C:\> dir | morecommands output directory list but display one page at a time.PowerShell vs Other Shells:
In Cmd.exe/Linux, output is treated as flat text.
In PowerShell, output is in the form of .NET objects with properties, methods, and events, enhancing data manipulation capabilities.
Example of Using the Pipeline
Commands for Services:
To get service info:
PS C:\> get-service –name bitsTo stop a service:
PS C:\> stop-service –name bitsUsing the pipeline for efficiency:
PS C:\> get-service –name bits | stop-service
Caution with Pipeline Usage
Incorrect usage like
get-service | stop-servicecould stop all services.Common Parameters:
-whatif: Shows what would happen if a cmdlet is executed.Example:
PS C:\> get-service | stop-service –whatif
-confirm: Prompts for each action. Respond with 'L' for "No to All."
Using Multiple Pipelines
Commands can be strung together:
cmdlet | cmdlet | cmdlet | cmdlet.Important to prioritize clarity over brevity in piping commands.
Storing Data with Out-File
Cmdlets can direct output to files for preservation:
To explore cmdlets for files:
PS C:\> get-help *file*Focus on outputting to a file:
PS C:\> get-help *out-*will showout-filecmdlet.
Example Usage:
PS C:\> Get-service | out-file –filepath c:\services.txtVerify file creation:
PS C:\> get-childitem –path c:\*.txt.
Read file contents:
PS C:\> get-content –path c:\services.txtView page by page:
PS C:\> get-content –path c:\services.txt | moreOpen in Notepad:
PS C:\> notepad c:\services.txt
Out-Printers and Output Verification
Use
out-printerto send content to a printer (Windows only):Command:
PS C:\> get-content –path c:\services.txt | out-printer
Working with Event Logs
Retrieve available logs:
PS C:\> get-eventlog –listCheck content of the System log:
PS C:\> get-eventlog –logname systemFilter by error entries:
PS C:\> get-eventlog –logname system –entrytype error
Limit results to the newest entries:
Example:
PS C:\> get-eventlog –logname system –entrytype error –newest 10
Direct output to file:
PS C:\> get-eventlog –logname system –entrytype error –newest 10 | out-file c:\error.txt
Exporting Data to CSV
Use
Export-Csvto save output in CSV format for easier analysis:Command:
PS C:\> get-service | export-csv –path c:\services.csv
Import CSV back into PowerShell:
PS C:\> import-csv –path c:\services.csv
Check the CSV in Notepad:
PS C:\> notepad c:\services.csvNote about header and delimiter specifics.
NoTypeInformation switch eliminates type information:
PS C:\> Get-service | export-csv –Path c:\services.csv -NoTypeInformation
XML Data Handling
Use
Export-CliXMLto export to XML file format, suitable for compatibility with databases:Command:
PS C:\> Get-process | export-clixml –path c:\process.xml
Re-import to PowerShell using:
PS C:\> import-clixml –path c:\process.xml
Comparing Data Sets
Use
Compare-Objectto find differences in two data sets:Command to create a reference file:
PS C:\> Get-process | export-clixml –path c:\refprocess.xmlSimulate differences by opening new applications (e.g., Notepad, Calculator).
Comparison command:
PS C:\> Compare-object –ReferenceObject (Import-Clixml c:\refprocess.xml) -DifferenceObject (Get-Process) –Property ProcessName
Additional Features: HTML Exporting
ConvertTo-HTMLcmdlet allows for generating HTML reports from PowerShell:Command:
PS C:\> Get-eventlog –logname system –newest 10 –entrytype error | ConvertTo-HTML | out-file –filepath c:\error.html
Customize the HTML output with titles, headers, and more:
Revised command with styling:
PS C:\> get-eventlog –logname system –newest 10 –entrytype error | convertto-html –Title "Errors" –Body (Get-Date) –Precontent "<P>This report was generated by John Doe</P>" | out-file -filepath c:\error.html
References
Ford, Jerry Lee, Jr. Microsoft Windows PowerShell Programming for the Absolute Beginner, Third Edition.
Hicks, Jeffery. "POWERSHELL FOR NEWBIES: Getting Started with PowerShell 4.0."
Jones, Don and Jeffery D. Hicks. Learn Windows PowerShell in a Month of Lunches.
Warner, Timothy L. Sams Teach Yourself Windows PowerShell in 24 Hours.