Are you Powershell newbie? It’s time to explain to you the Powershell Pipeline. It’s useful for many scenarios. Thanks to Powershell Pipeline, your code will be shorter and more understandable. Before you begin using it, you have to learn what it is and when you can use it. 

You can read Microsoft’s description, but I know that for many beginners, this article is not understandable. I will try to explain this topic to you in my way. Let’s start.

Calculate tax with Powershell Pipeline

Many times I explain Powershell topics using real-life examples. Many readers value these articles, so now I will use real-life examples too. 

Nobody likes to pay taxes, but everyone has to. I will use tax calculation as an example. 

Please imagine that you get a $10000 income. Now you have to calculate 18% tax. Let’s do this with Powershell. I wrote two functions below.

function Get-Income {
    param(
        [Parameter(Mandatory=$true)]
        [int]$Dollars

    )

    Write-Host "You get $Dollars income"

    return $Dollars
}

function Calculate-Tax {
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline)]
        [int]$Income
    )

    Write-Host "18% Tax calculation.."
    $Tax = $Income * 0.18

    Write-Host "You have to pay `$$Tax tax"

    return $Tax
}

You can use these Powershell functions to calculate tax. Let’s see the first example.

$Income = Get-Income -Dollars 10000
Calculate-Tax -Income $Income
Powershell Pipeline

As you can see, I assigned the result of the first function to the variable and used it as a parameter in the next one.

Using Powershell pipeline, you can skip the variable usage.

Get-Income -Dollars 10000 | Calculate-Tax
Powershell Pipeline

As you can see above, the Powershell Pipeline gets the result of one command and redirect it to the next one. 

Conclusion

In conclusion, the pipeline is very helpful when you would like to write shorter scripts. Above, I showed you the basics usage of the pipeline. Now you can try to use it in your Powershell scripts.

If my article helped you, please leave a comment and subscribe to be always up to date.

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.