0% found this document useful (0 votes)
0 views

powershell pipeline

This document provides an overview of working with the Windows PowerShell pipeline, covering key concepts such as understanding the pipeline, selecting, sorting, and measuring objects, filtering objects, and enumerating them. It includes lessons on formatting output, creating calculated properties, and sending pipeline data to various formats like CSV, XML, and JSON. Additionally, it features demonstrations and lab scenarios to reinforce learning and practical application of PowerShell commands.

Uploaded by

rajbharathi821
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

powershell pipeline

This document provides an overview of working with the Windows PowerShell pipeline, covering key concepts such as understanding the pipeline, selecting, sorting, and measuring objects, filtering objects, and enumerating them. It includes lessons on formatting output, creating calculated properties, and sending pipeline data to various formats like CSV, XML, and JSON. Additionally, it features demonstrations and lab scenarios to reinforce learning and practical application of PowerShell commands.

Uploaded by

rajbharathi821
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

powershell

Working with the Windows


PowerShell pipeline
Module Overview

Understanding the pipeline


Selecting, sorting, and measuring objects
Filtering objects out of the pipeline
Enumerating objects in the pipeline
• Sending pipeline data as output
Lesson 1: Understanding the pipeline

What is the pipeline?


Pipeline output
Discovering object members
Demonstration: Viewing object members
Formatting pipeline output
• Demonstration: Formatting pipeline output
What is the pipeline?

• Windows PowerShell runs commands in a


pipeline
• Each console command line is a pipeline
• Commands separated by a pipe character
(|)
• Commands execute from left to right
• Output of each command is piped
(passed) to the next
• The output of the last command in the
pipeline is what appears on your screen
• Piped commands typically follow the
pattern Get |Set, Get | Where, or Select
Pipeline output

• Windows PowerShell commands produce


objects as their output
• An object is like a table of data in memory
• Allows the Get | Set pattern to work

Property
Collection

Name Status DisplayNam


Object e
WinRM Running Windows
Remote
Management
Discovering object members

• Object members include:


• Properties
• Methods
• Events

• Run a command that produces an object,


and pipe that object to Get-Member to see
a list of members
• Get-Member is a discovery tool, similar to
Help, that can help you learn to use the
shell
Demonstration: Viewing object members

In this demonstration, you will see how to


run commands in the pipeline and how to
use Get-Member
Formatting pipeline output

• Use the following cmdlets to format pipeline


output:
• Format-List
• Format-Table
• Format-Wide

• The -Property parameter:


• Is common to all formatting cmdlets
• Filters output to specified property names
• Can only specify properties that were passed to
the formatting command
Demonstration: Formatting pipeline output

In this demonstration, you will see how to


format pipeline output
Lesson 2: Selecting, sorting, and measuring
objects

Sorting objects by a property


Demonstration: Sorting objects
Measuring objects
Demonstration: Measuring objects
Selecting a subset of objects
Selecting properties of objects
Demonstration: Selecting objects
Creating calculated properties
• Demonstration: Creating calculated
properties
Sorting objects by a property

• Each command determines its own default


sort order
• Sort-Object can resort objects in the
pipeline
• Here is an example:
• Get-Service | Sort-Object Name –Descending
• Sorting enables grouping output by using:
• The -GroupBy parameter
• The Group-Object command
Demonstration: Sorting objects

In this demonstration, you will see how to


sort objects by using the Sort-Object
command
Measuring objects

• Measure-Object accepts a collection of objects


and counts them
• Add -Property to specify a single numeric property,
and then add:
• -Average to calculate an average
• -Minimum to display the smallest value
• -Maximum to display the largest value
• -Sum to display the sum
• The output is a measurement object, and not
whatever you piped in

Get-ChildItem -File | Measure -Property Length


-Sum -Average -Minimum -Max
Demonstration: Measuring objects

In this demonstration, you will see how to


measure objects by using the Measure-
Object command
Selecting a subset of objects

• One of two uses for Select-Object


• Use parameters to select the specified
number of rows from the beginning or end
of the piped-in collection:
• -First for the beginning
• -Last for the end
• -Skip to skip a number of rows before selecting
• -Unique to ignore duplicated rows

• You cannot specify any criteria for choosing


specific rows
Selecting properties of objects

• The second use of Select-Object


• Use the -Property parameter to specify a
comma-separated list of properties (wildcards
are accepted) to include
• You can combine the -Property parameter with
-First, -Last, and -Skip to select a subset of
rows
Demonstration: Selecting objects

In this demonstration, you will see various


ways to use the Select-Object command
Creating calculated properties

• Calculated (custom) properties let you


choose the output label and contents
• Each calculated property works like a single
regular property in the property list
accepted by Select-Object
• Create calculated properties by using a
specific syntax:
• Label defines the property name
• Expression defines the property contents
• Within the expression, $PSItem (or $_) refers to
the piped-in object
Creating calculated properties
Calculated property hash table
Hash table

Label key Label string


value

@{
n='VirtualMemory';
e={ $PSItem.VM }
} Semicolon

Expression Expression
key script block
Creating calculated properties

To format calculated properties:


• Use shortcuts for specific memory amounts:
• KB for kilobytes
• MB for megabytes
• GB for gigabytes
• TB for terabytes
• PB for petabytes

• Use the -F format operator to format


numbers with a specified number of decimal
places
Creating calculated properties

Example: Calculated properties

Get-Volume |
Select-Object –Property DriveLetter,
@{
n='Size(GB)';
e={'{0:N2}' -f ($PSItem.Size / 1GB)}
},
@{
n='FreeSpace(GB)';
e={'{0:N2}' -f ($PSItem.SizeRemaining / 1GB)}
}
Demonstration: Creating calculated
properties

In this demonstration, you will see how to


use Select-Object to create calculated
properties. You will then see how those
calculated properties behave like regular
properties.
Lab Review

Suppose that you want to produce output


that includes all of an object’s properties
except one. What is the most efficient way to
do that?
• List the basic formatting commands, and
explain why you might use each one.
Lesson 3: Filtering objects out of the
pipeline

Comparison operators
Basic filtering syntax
Advanced filtering syntax
Demonstration: Filtering
• Optimizing filtering performance
Comparison operators

Comparison Case-insensitive Case-sensitive


type operator operator
Equality -eq -ceq
Inequality -ne -cne
Greater than -gt -cgt
Less than -lt -clt
Greater than or -ge -cge
equal to
Less than or -le -cle
equal to
Wildcard -like -clike
equality
Basic filtering syntax

• The Where-Object command provides


filtering
• Here are examples of basic syntax:

Get-Service |
Where Status –eq Running

Get-Process |
Where CPU –gt 20
Basic filtering syntax

Limitations of the basic syntax:


• It supports only a single comparison―you
cannot compare two things
• It does not support property
dereferencing―you can refer to only direct
properties of the object piped into the
command
• This will not work:
• Get-Service | Where Name.Length –gt 5
Advanced filtering syntax

• Supports multiple conditions and has no


restrictions on what kinds of expressions
you
can use
• Requires a filter script that contains your
filtering criteria and that evaluates to either
True or False
• Inside the filter script, use $PSItem or $_ to
refer to the object that was piped into the
command
Advanced filtering syntax

Here are three examples of advanced filtering:


• Get-Service |
Where-Object –Filter {$PSItem.Status –eq 'Running' }
• Get-Service | Where { $_.Status –eq 'Running' }
• Get-Service | ? { $PSItem.Status –eq 'Running' }
Advanced filtering syntax

• Use the Boolean operators -and and -or to


combine multiple comparisons into a single
expression:

Get-Volume | Where-Object –Filter {


$PSItem.HealthStatus –ne 'Healthy'
-or
$PSItem.SizeRemaining –lt 100MB
}
Demonstration: Filtering

In this demonstration, you will see various


ways to filter objects out of the pipeline
Optimizing filtering performance

• To improve performance, move filtering as


close to the beginning of the command line
as possible
• Some commands have parameters that can
do some filtering for you, so whenever
possible, use those parameters instead of
Where-Object
Lab Scenario

You are continuing your project to create


management reports by using Windows
PowerShell. You have to retrieve additional
management information about the
computers in your environment. You need
the output of your commands to include
only specified information and objects.
Lab Review

Do you prefer the basic or advanced syntax


of Where-Object?
What is the difference between Select-
Object and Where-Object?
• In the first task of this lab, were you able to
achieve the goal without using the Where-
Object command?
Lesson 4: Enumerating objects in the
pipeline

Purpose of enumeration
Basic enumeration syntax
Demonstration: Basic enumeration
Advanced enumeration syntax
• Demonstration: Advanced enumeration
Purpose of enumeration

• To take a collection of objects and:


• Run an action on each item
• Process them one at a time
• Not necessary when Windows PowerShell has a
command that can perform the action you need
• Useful when an object has a method that does
what you want, but Windows PowerShell does not
offer an equivalent command
Basic enumeration syntax

Get-ChildItem –Path C:\Example –File |


ForEach-Object –MemberType Encrypt

Get-ChildItem –Path C:\Example –File |


ForEach Encrypt

Get-ChildItem –Path C:\Example –File |


% –MemberType Encrypt
Basic enumeration syntax

Limitations:
• Can access only a single member (method
or property) of the objects that were piped
into the command
• Cannot:
• Run commands or code
• Evaluate expressions
• Make logical decisions
Demonstration: Basic enumeration

In this demonstration, you will see how to


use the basic enumeration syntax to
enumerate several objects in a collection
Advanced enumeration syntax

• Allows you to perform any task by writing


commands in a script block
• Uses $PSItem or $_ to reference the
objects that were piped into the command:
• Get-ChildItem C:\Test –File |
ForEach-Object { $PSItem.Encrypt() }
• Has additional parameters that allow you to
specify actions to take before and after the
collection of objects is processed
Demonstration: Advanced enumeration

In this demonstration, you will see two ways


to use the advanced enumeration syntax to
perform tasks on several objects
Lab C: Enumerating objects

• Exercise 1: Enumerating objects

Logon Information
Virtual machines: 10961C-LON-DC1
10961C-LON-CL1
User name: Adatum\
Administrator
Password: Pa55w.rd
Estimated Time: 30 minutes
Lab Scenario

You are a network administrator for Adatum


Corporation. You were recently made
responsible for managing the infrastructure
for the London branch office. You have to
complete several management tasks by
using Windows PowerShell. These tasks
require you to perform actions on multiple
objects.
Lab Review

• Do you prefer the basic or advanced syntax


of ForEach-Object?
Lesson 5: Sending pipeline data as output

Writing output to a file


Converting output to CSV
Converting output to XML
Converting output to JSON
Converting output to HTML
Demonstration: Exporting data
• Additional output options
Writing output to a file

• Out-File writes whatever is in the pipeline to a text


file
• The > and >> redirection operators are also
supported
• The text file is formatted exactly the same as the
data would have appeared on the screen―no
conversion to another form occurs
• Unless the data has been converted to another
form, the resulting text file is usually suitable for
viewing only by a person
• As you start to build more complex commands, you
need to keep track of what the pipeline contains at
each step
Converting output to CSV

• The commands are:


• ConvertTo-CSV
• Export-CSV

• The commands send:


• Properties as headers
• No type information

• You can easily open large CSV files in Excel


Converting output to XML

• ConvertTo-CliXml
• Export-CliXml
• Portable data format
• Multivalue properties become individual
entries
Converting output to JSON

• The command is
• ConvertTo-JSON

• The advantages are:


• Compactness
• Ease of use, especially with JavaScript
• A format like a hash table
Converting output to HTML

• The command is ConvertTo-HTML


• The command creates a table or list in
HTML
• You must pipe the output to a file
• The parameters include:
• -Head
• -Title
• -PreContent
• -Postcontent
Demonstration: Exporting data

In this demonstration, you will see different


ways to convert and export data
Additional output options

• Out-Host allows more control of on-screen


output
• Out-Printer sends output to a printer
• Out-GridView creates an interactive,
spreadsheet-like view of the data
Lab D: Sending output to a file

• Exercise 1: Converting objects

Logon Information
Virtual machines: 10961C-LON-DC1
10961C-LON-CL1
User name: Adatum\
Administrator
Password: Pa55w.rd
Estimated Time: 30 minutes
Lab Scenario

You need to change some information about


Active Directory users and create a report of
those changes. You are evaluating data
formats for reporting the information
changes. You must convert data to different
formats and decide which ones are the most
appropriate for your needs.
Lab Review

Can you use ConvertTo-Csv or Export-Csv to


create a file delimited by a character other
than a comma? For example, can you create
a tab-delimited file?
• The HTML data produced by ConvertTo-Html
looks very plain. The HTML standard offers a
way to specify visual styles for an HTML
document. This is known as Cascading Style
Sheets (CSS). Does the command offer a
way to attach a style sheet?
Module Review and Takeaways

Review Question
Real-world Issues and Scenarios
Best Practice
• Common Issues and Troubleshooting Tips

You might also like