Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save craig-martin/3612c4a43734fafb5dff to your computer and use it in GitHub Desktop.
Save craig-martin/3612c4a43734fafb5dff to your computer and use it in GitHub Desktop.
PowerShell sample showing how to use a function with pipeline input by parameter name
### Create a sample CSV file
@'
ColumnA,ColumnB,ColumnC
'row1ColA','row1ColB','row1ColC'
'row2ColA','row2ColB','row2ColC'
'row3ColA','row3ColB','row3ColC'
'@ | Out-File -FilePath (Join-Path $env:TEMP testFoo.csv)
### Import the sample CSV file
Import-Csv -Path (Join-Path $env:TEMP testFoo.csv)
function Test-ValueFromPipelineByPropertyName
{
<#
.Synopsis
Short description
.EXAMPLE
Test-ValueFromPipelineByPropertyName -ColumnA foo -ColumnB bar -ColumnC baz
.EXAMPLE
### Create a sample CSV file
@'
ColumnA,ColumnB,ColumnC
'row1ColA','row1ColB','row1ColC'
'row2ColA','row2ColB','row2ColC'
'row3ColA','row3ColB','row3ColC'
'@ | Out-File -FilePath (Join-Path $env:TEMP testFoo.csv)
Import-Csv -Path (Join-Path $env:TEMP testFoo.csv) | Test-ValueFromPipelineByPropertyName
#>
[CmdletBinding()]
Param
(
# ColumnA help description
[Parameter(ValueFromPipelineByPropertyName=$true)]
$ColumnA,
# ColumnB help description
[Parameter(ValueFromPipelineByPropertyName=$true)]
$ColumnB,
# ColumnC help description
[Parameter(ValueFromPipelineByPropertyName=$true)]
$ColumnC
)
Process
{
Write-Host "$ColumnA $ColumnB $ColumnC"
}
}
### Call the function with parameters
Test-ValueFromPipelineByPropertyName -ColumnA foo -ColumnB bar -ColumnC baz
### Call the function with pipeline input (by parameter name)
Import-Csv -Path (Join-Path $env:TEMP testFoo.csv) | Test-ValueFromPipelineByPropertyName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment