# Feeding switch parameter from pipeline input

**URL:** <https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425>\
**Category:** PowerShell Help\
**Created:** [December 23, 2015, 8:58pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425 "2015-12-23T20:58:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![savindrasingh-shahoo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/savindrasingh-shahoo/32/45_2.png) [@savindrasingh-shahoo](https://forums.powershell.org/u/savindrasingh-shahoo)\
**Post date:** [December 23, 2015, 8:58pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/1 "2015-12-23T20:58:00Z")

</div>

Hello experts, I need to pass parameters to a script from pipeline input by importing the required values from CSV file. The original script has more than 15 parameters to be passed and input values are stored in a CSV file. I am posting a simple example to express the issue.

Below are the contents of CSV file (Input.csv)  
`
ResourceGroupName,SetThrottling
TestRG1,1
TestRG2,0
TestRG3,1
TestRG4,0
TestRG5,0
`

Script file - Switch-FromPipelineTest.ps1  
`
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[String]$ResourceGroupName,`

`
```
[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[Switch]$SetThrottling

```
)
Begin {}
Process
{
Function TestingValues
{
Param
(
$ResourceGroupName,
$SetThrottling
)
```
    Write-Host "$ResourceGroupName is set to $SetThrottling"
}

TestingValues -ResourceGroupName $ResourceGroupName -SetThrottling $SetThrottling

```
`

`}
End {}
`

If I run the command `Import-Csv .\Input.csv | .\Switch-FromPipelineTest.ps1 ` it gives an error as given below:

`C:\Scripts\Switch-FromPipelineTest.ps1 : Cannot process argument transformation
on parameter ‘SetThrottling’. Cannot convert value “System.String” to type
“System.Management.Automation.SwitchParameter”. Boolean parameters accept only
Boolean values and numbers, such as $True, $False, 1 or 0.
At line:1 char:26`

`
`
`
Import-Csv .\Input.csv | .\Switch-FromPipelineTest.ps1
`- `
```
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```
`
`
CategoryInfo : InvalidData: (@{ResourceGroup…etThrottling=1}😛
SObject) [Swith-FromPipelineTest.ps1], ParameterBindingArgumentTransformati
onException
` - `FullyQualifiedErrorId : ParameterArgumentTransformationError,Switch-FromPi
pelineTest.ps1`

In order to make this work, I have to run below command:  
`Import-Csv -Path .\Input.csv -Verbose | Select-Object -Property ResourceGroupName,@{n=‘SetThrottling’;e={[bool][int]$_.SetThrottling}} | .\Switch-FromPipelineTest.ps1`

Is there a way we can omit the type casting done by using custom property expression in the second command? as in the original script, I have several [switch] parameters and I need to do the same thing for each [switch] parameter.

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [December 23, 2015, 9:54pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/2 "2015-12-23T21:54:11Z")

</div>

remove ValueFromPipeline=$True from both parameters  
and read some tips here

> **[Tips on Implementing Pipeline Support](https://learn-powershell.net/2013/05/07/tips-on-implementing-pipeline-support/)**
>
> Something that I was seeing during the first event of the Scripting Games was the use (or misuse) of implementing pipeline support for a parameter in a function or script. While most people did do …

todo: move function TestingValues to begin{} block

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 23, 2015, 11:19pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/3 "2015-12-23T23:19:49Z")

</div>

Hmm, I’ve never tried to do a switch parameter from the pipeline. However, when you’re using splatting, you can do switches with $true or $false, so maybe it’ll work in the pipeline if you make it a [bool] instead of [int].

---

<div class="post-metadata">

**Author:** ![savindrasingh-shahoo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/savindrasingh-shahoo/32/45_2.png) [@savindrasingh-shahoo](https://forums.powershell.org/u/savindrasingh-shahoo)\
**Post date:** [December 24, 2015, 5:02pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/4 "2015-12-24T17:02:09Z")

</div>

@Max Kozlov: I went through that link earlier and also tried to remove ValueFromPipeline=$True from both parameters, it didn’t help. I still need to manually convert the value from CSV column to [Switch] / [bool] before passing it to the script.

@Dave Wyatt: I can execute the script if I use:  
`Import-Csv -Path .\Input.csv -Verbose | Select-Object -Property ResourceGroupName,@{n=‘SetThrottling’;e={[bool][int]$_.SetThrottling}} | .\Switch-FromPipelineTest.ps1`

But I want to get rid of this extra layer of conversion using Select-Object, since my actual script has now around 30 parameters that I need to pass using CSV and there are many switch parameters that I have to convert like this if I don’t get a solution.

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [December 24, 2015, 5:15pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/5 "2015-12-24T17:15:25Z")

</div>

sorry, does not test solution before…  
also you need to change or remove parameter types from outer proxy function and do type conversion inplace  
this variant works well

```
[CmdletBinding()]
 Param
 (
 [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)]
 [string]$ResourceGroupName,

[Parameter(ValueFromPipelineByPropertyName=$True)]
 [int]$SetThrottling
 )

Begin {
 Function TestingValues
 {
 Param
 (
 [string]$ResourceGroupName,
 [switch]$SetThrottling
 )

Write-Host "$ResourceGroupName is set to $SetThrottling"
 }
}
 Process
 {
	TestingValues -ResourceGroupName $ResourceGroupName -SetThrottling:([bool]$SetThrottling)
 }
 End {}
```

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [December 24, 2015, 7:35pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/6 "2015-12-24T19:35:54Z")

</div>

btw, import-csv afaik always import values as string thus you always need conversion as you do with select, as I suggest with inplace conversion or by using foreach-object:

```
Import-Csv -Path .\Input.csv -Verbose | Foreach-Object { $_.SetThrottling=[bool][int]$_.SetThrottling; $_ } | .\Switch-FromPipelineTest.ps1
```

if you need to convert many parameters you can do this in cycle like

```
$toconvert = 'SetThrottling','param1','param2'

Import-Csv -Path .\Input.csv -Verbose | Foreach-Object {
foreach ($param in $toconvert) {
$_.$param = [bool][int]$_.$param 
}
$_
} |
 .\Switch-FromPipelineTest.ps1
```

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [December 24, 2015, 10:28pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/7 "2015-12-24T22:28:51Z")

</div>

As Max said, the main problem here is that Import-Csv makes all values from the CSV file as String values, and powershell obviously does not like trying to convert the string values to bool.

I too would recommend just placing a Select-Object between your Import-Csv and your function with a calculated property to try to convert the property from the CSV file into a Boolean value. It may seem like unnecessary work but unfortunately it’s due to the way the Import-Csv cmdlet was designed (to this point at least).

PS Merry Christmas!!

---

<div class="post-metadata">

**Author:** ![savindrasingh-shahoo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/savindrasingh-shahoo/32/45_2.png) [@savindrasingh-shahoo](https://forums.powershell.org/u/savindrasingh-shahoo)\
**Post date:** [December 25, 2015, 2:00pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/8 "2015-12-25T14:00:18Z")

</div>

Thank you all for your inputs. After looking at all possibilities I think I have to convert the switch parameter to a string parameter by sung a validate set of “Yes” and “No”. I will change the CSV format to use Yes and NO instead of 1 and 0.

In script I will check the value using If condition whether $SetThrottling = Yes or No.

That seems to be the only option for now as there is no other way we can pass Bool values through pipeline while importing CSV.

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [December 26, 2015, 2:42pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/9 "2015-12-26T14:42:49Z")

</div>

(Post update: Scratch that, I see the problem and the below does not help the issue)

@savindrasingh-shahoo use [System.Convert]::ToBoolean()

[https://social.technet.microsoft.com/Forums/windowsserver/en-US/300b5602-b16a-4a10-96fd-3e2e6ba29ed6/importcsv-with-true-and-false-values?forum=winserverpowershell](https://social.technet.microsoft.com/Forums/windowsserver/en-US/300b5602-b16a-4a10-96fd-3e2e6ba29ed6/importcsv-with-true-and-false-values?forum=winserverpowershell)

```
[pscustomobject]@{
    Value1 = $true
    Value2 = $false
    Value3 = "string"
} | Export-Csv test.csv

$a = Import-Csv test.csv

[System.Convert]::ToBoolean($a.Value1)
[System.Convert]::ToBoolean($a.Value2)
[System.Convert]::ToBoolean($a.Value3)

($a.Value1).GetType()
($a.Value2).GetType()
([System.Convert]::ToBoolean($a.Value1)).GetType()
([System.Convert]::ToBoolean($a.Value2)).GetType()
```

**Results:**

```
True
False
Exception calling "ToBoolean" with "1" argument(s): "String was not recognized as a valid Boolean."
At line:11 char:1
+ [System.Convert]::ToBoolean($a.Value3)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

IsPublic IsSerial Name BaseType                                                                                                                  
-------- -------- ---- --------                                                                                                                  
True True String System.Object                                                                                                             
True True String System.Object                                                                                                             
True True Boolean System.ValueType                                                                                                          
True True Boolean System.ValueType
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:43pm UTC](https://forums.powershell.org/t/feeding-switch-parameter-from-pipeline-input/5425/10 "2024-05-16T20:43:21Z")

</div>


