TECHDIIP

  • Tech
  • Windows
  • Powershell
  • Scripts
  • News
  • Games

Powershell Pipeline

4 November 2020 admin Leave a Comment

PowerShell Pipeline Concept, the powershell one of the main feature and the most powerful element is a pipeline that enables administrators to write the code easily.

When you club two commands, powershell has to find out how to get the output of the first command to the input of the second command, that’s where the pipeline concept comes.  Let’s learn using examples.

Get-content -patch “c:\c:\serverslist.txt | get-service

The file serverslist.txt has many servers and pipeline in powershell accepts one server object in the loop and gets the services on those servers.

Using pipelines we can do a lot of things, we can sort, filter, and remove the objects.

Get-service | sort-object Name

OutPut:

status

Name

DisplayName

Running

AvastWscReporter

AvastWscReporter

Stopped

AxInstSV

ActiveX Installer (AxInstSV)

Stopped

BcastDVRUserSer…

GameDVR and Broadcast User Service_…

Running

bthserv

Bluetooth Support Service

Running

camsvc

Capability Access Manager Service

Stopped

CaptureService_…

C

Get-service | select-object Name, status

Passing multiple pipelines in a single line of code to achieve only running services.

Get-service | select-object Name, status | where-object {$_.status -eq “running”}

Output:

Name

State

dobeARMservice

Running

Appinfo

Running

aswbIDSAgent

Running

AudioEndpointBuilder

Running

Audiosrv

Running

avast!

Running

AvastWscReporter

Running

BcmBtRSupport

Running

BFE

Running

BITS

Running

BrokerInfrastructure

Running

Note: There are some commands do not accept the pipeline, we will learn more in our upcoming topics.

PowerShell most commonly used commands in every script.

Powershell Write-Host

This PS command allows us to write the customized output to a host, you can also specify the color to output text by using its parameter -foregroundcolor and it allows us to change the background color of output text using the parameter -backgroundcolor.

Write-host "I'm learning Powershell"

Write-host "I'm learning Powershell" -foregroundcolor -cyan

Write-host "I'm learning Powershell" -ForegroundColor DarkGreen -BackgroundColor White

Powershell Read-Host

This Command reads the line of input that you give from the console.

Examples

$myage = Read-Host "Please enter your age"

$number= Read-Host "Please choose the option 1…10"

$mypassowrd = Read-Host "Enter a Password" -AsSecureString

Powershell Sort-Object

This sort-object command sorts the objects based on the object property values like ascending or descending order.

Examples

Get-Process | Sort-Object -Descending

Get-Process | Sort-Object -Property name

Get-Process | Sort-Object -Unique

Powershell Format-Table

This command formats the output of another command into a table format with selected properties of the object in each column.

Examples

Get-service | format-table -autosize

Get-Process | Format-Table -Property name, cpu, id

Get-Process | Format-Table -GroupBy name

Powershell Start-Sleep

This command suspends the activity in a script, basically, it waits as per the specified time.

Syntax.

Start-Sleep

[-Seconds] <Double> [<CommonParameters>]

Example.

Start-sleep -s 10

Powershell Out-file Command

This command used for writing the out to a text file and you need to specify the file path as where to store.

Syntax

Out-File | -filepath C:\temp

Get-process | out-file -filepath C:\temp\process.txt.

Get-process | out-file -filepath C:\temp\process.txt -append

Get-service| out-file -filepath C:\temp\service.txt -force

Powershell Export-Csv Command

This command converts objects into a series of comma-separated value string and creates a file.

Examples

Get-service | export-csv -path C:\temp\raj.csv Get-service | export-csv -path C:\temp\raja.csv – NoTypeInformation #The NoTypeinformation parameter removes #TYPE information header from the output file

Thank you for reading this article, if you have any questions please let us know.

Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.

Scripts

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Techdiip © 2012 - 2020 | Please Read Our

  • Privacy Policy
  • Disclaimer – User Agreement
  • About Us
  • Contact Us