You are on page 1of 18

BCAICT-503

POWERSHELL SCRIPTING

MODULE 2 : The PowerShell Pipeline


Instructor – Gajendra Shrimal
Pipeline ??
• Short description
– Combining commands into pipelines in the PowerShell
• Long description
– A pipeline is a series of commands connected by pipeline
operators (|) (ASCII 124).
– Each pipeline operator sends the results of the preceding
command to the next command.
– The output of the first command can be sent for processing as
input to the second command. And that output can be sent to
yet another command. The result is a complex command chain
or pipeline that is composed of a series of simple commands.
Syntax
• PowerShell
• Command-1 | Command-2 | Command-3
• For example
– Get-Process notepad | Stop-Process

The first command uses the Get-Process cmdlet to get an


object representing the Notepad process. It uses a
pipeline operator (|) to send the process object to
the Stop-Process cmdlet, which stops the Notepad
process.
pipeline example gets the text files in the current directory, selects only the files that are
more than 10,000 bytes long, sorts them by length, and displays the name and length of
each file in a table.

Get-ChildItem -Path *.txt

Get-ChildItem -Path *.txt | | (FileInfo objects for *.txt)


V
Where-Object {$_.length -gt 10000}
Where-Object {$_.length -gt 10000} | | (FileInfo objects for *.txt)
| ( Length > 10000 )
Sort-Object -Property length | V
Sort-Object -Property Length

Format-Table -Property name, length | (FileInfo objects for *.txt)


| ( Length > 10000 )
| ( Sorted by length )
V
Format-Table -Property name, length
| (FileInfo objects for *.txt)
| ( Length > 10000 )
| ( Sorted by length )
| ( Formatted in a table )
V

Name Length
---- ------
tmp1.txt 82920
tmp2.txt 114000
tmp3.txt 114000
Pipe and Sort
The list is sorted alphabetically
◦ according to the process name.
You can sort WS field
◦ According to the memory usage
To do this the user
◦ “pipes” the output of ps
◦ to a second Cmdlet “sort-object”

ps | sort-object WS –descending
Sorting wrt Memory Usage
Filtering & Splitting the Commands
User can filter that
◦ the “powershell” not to be seen in output
 Add a filter
piped Cmdlets can be
◦ split over multiple lines:

ps | where-object –FilterScript
{$_.processname -ne "powershell"}
| sort-object WS –descending
Using pipelines
• Most PowerShell cmdlets are designed to
support pipelines. In most cases, you
can pipe the results of a Get cmdlet to another
cmdlet of the same noun. For example, you can
pipe the output of the Get-Service cmdlet to
the Start-Service or Stop-Service cmdlets.
• Get-Service wmi | Start-Service
• This example pipeline starts the WMI service on
the computer
• For another example, you can pipe the output
of Get-Item or Get-ChildItem within the
PowerShell registry provider to the New-
ItemProperty cmdlet. This example adds a new
registry entry, NoOfEmployees, with a value
of 8124, to the MyCompany registry key.
• Get-Item -Path HKLM:\Software\MyCompany |
New-ItemProperty -Name NoOfEmployees -
Value 8124
• Many of the utility cmdlets, such as Get-
Member, Where-Object, Sort-Object, Group-
Object, and Measure-Object are used almost
exclusively in pipelines. You can pipe any
object type to these cmdlets. This example
shows how to sort all the processes on the
computer by the number of open handles in
each process.
• Get-Process | Sort-Object -Property handles
• You can pipe objects to the formatting, export,
and output cmdlets, such as Format-
List, Format-Table, Export-Clixml, Export-CSV,
and Out-File.
• This example shows how to use the Format-
List cmdlet to display a list of properties for a
process object.
• Get-Process winlogon | Format-List -Property *
Methods of accepting pipeline input
Cmdlets parameters can accept pipeline input in one of two
different ways:
• ByValue: The parameter accepts values that match the
expected .NET type or that can be converted to that type.
– For example, the Name parameter of Start-Service accepts pipeline
input by value. It can accept string objects or objects that can be
converted to strings.
• ByPropertyName: The parameter accepts input only when the
input object has a property of the same name as the parameter.
– For example, the Name parameter of Start-Service can accept objects
that have a Name property. To list the properties of an object, pipe it
to Get-Member.
Filtered
To concatenation symbol
The concatenation symbol (>)
◦ will send the result output file
◦ after the information has been converted to HTML
◦ so that it can then be viewed in a web browser:

ps |
where-object -FilterScript
{$_.processname -ne "powershell"} |
sort-object WS –descending |
convertto-html -property Name,
Displaying in Browser
Find all the processes on a computer that
started today
Get-Process | Where {$_.starttime -ge
[datetime]::today}
Find the processes that use more than 1000
MB of memory and kill them
get-process | where-object { $_.WS -gt
1000MB } | stop-process -whatif
Calculate the number of bytes in the files in a
directory
get-childitem | measure-object -property
length -sum

You might also like