Saturday 10 April 2021

Pipeline ByValue and ByPropertyName

 Accept pipeline input ByValue

Get-Process|Gm

Will return TypeName: System.Diagnostics.Process


Thats why Get-Process -Name notepad|Stop-Process Works. 

Get-Process (Retrive Type Process Objects)

Stop-Process (Accepts Type Process Objects)

Its mapped to the below parameter which is ByValue

-InputObject <Process[]>

    Required?                    true

    Position?                    Named

    Accept pipeline input?       true (ByValue)

    Parameter set name           InputObjectWithUserName, InputObject

    Aliases                      None

    Dynamic?                     false


 Stop-Process [-InputObject] <Process[]> [-PassThru] [-Force] [-WhatIf] [-Confirm]  [<CommonParameters>]

By default pipeline uses ByValue and if that is not available then its uses ByPropertyName 


Accept pipeline input ByPropertyName

"notepad"|Stop-Process will Fail.

Stop-Process : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the

parameters that take pipeline input.

"notepad" : Considered as byValue and Stop-Process Accept pipeline input ByPropertyName

-Name <string[]>

    Required?                    true

    Position?                    Named

    Accept pipeline input?       true (ByPropertyName)

    Parameter set name           Name

    Aliases                      ProcessName

    Dynamic?                     false


Stop-Process -Name <string[]> [-PassThru] [-Force] [-WhatIf] [-Confirm]  [<CommonParameters>]


But we can force it by using the below command

[pscustomobject]@{name="notepad"}|Stop-Process. This time it will Accept pipeline input ByPropertyName


No comments:

Post a Comment