142

I have to invoke a PowerShell script from a batch file. One of the arguments to the script is a boolean value:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify $false

The command fails with the following error:

Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.

At line:0 char:1
+  <<<< <br/>
+ CategoryInfo          : InvalidData: (:) [RunScript.ps1], ParentContainsErrorRecordException <br/>
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,RunScript.ps1

As of now I am using a string to boolean conversion inside my script. But how can I pass boolean arguments to PowerShell?

11 Answers 11

252

A more clear usage might be to use switch parameters instead. Then, just the existence of the Unify parameter would mean it was set.

Like so:

param (
  [int] $Turn,
  [switch] $Unify
)
5
  • 35
    This should be the accepted answer. To further the discussion, you can set a default value like any other parameter like so: [switch] $Unify = $false Commented May 14, 2015 at 22:00
  • I overlooked this answer, as I was sure Boolean was the way to go. It is not. Switch encapsulates the functionality of the boolean, and stops these kind of errors: " Cannot convert value "False" to type "System.Management.Automation.ParameterAttribute". Switches worked for me.
    – TinyRacoon
    Commented Feb 8, 2016 at 11:00
  • 6
    @MarioTacke If you don't specify the switch parameter when invoking the script then it is automatically set to $false. So there's no need to explicitly set a default value.
    – doubleDown
    Commented Mar 13, 2018 at 2:21
  • This suggestion worked very well; was able to update one of the params from [bool] to [switch] and this successfully allowed me to pass the argument via Task Scheduler. Commented Sep 28, 2018 at 17:47
  • If you are using the [Parameter] attribute on any of the parameters, you have to make sure that you pass the switch parameters as switches and not as booleans. So e.g. -Unify for true and nothing for false. Otherwise -Unify $false or -Unify $true would raise error "A positional parameter cannot be found that accepts argument '$true'" Commented May 3 at 15:50
77

It appears that powershell.exe does not fully evaluate script arguments when the -File parameter is used. In particular, the $false argument is being treated as a string value, in a similar way to the example below:

PS> function f( [bool]$b ) { $b }; f -b '$false'
f : Cannot process argument transformation on parameter 'b'. Cannot convert value 
"System.String" to type "System.Boolean", parameters of this type only accept 
booleans or numbers, use $true, $false, 1 or 0 instead.
At line:1 char:36
+ function f( [bool]$b ) { $b }; f -b <<<<  '$false'
    + CategoryInfo          : InvalidData: (:) [f], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,f

Instead of using -File you could try -Command, which will evaluate the call as script:

CMD> powershell.exe -NoProfile -Command .\RunScript.ps1 -Turn 1 -Unify $false
Turn: 1
Unify: False

As David suggests, using a switch argument would also be more idiomatic, simplifying the call by removing the need to pass a boolean value explicitly:

CMD> powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify
Turn: 1
Unify: True
2
  • 7
    For those (like me) who have to keep the "-File" parameter, and can't change to a switch argument, but do have control over the string values that are sent, then the simplest solution is to convert the parameter to a boolean using [System.Convert]::ToBoolean($Unify); the string values must then be "True" or "False". Commented Apr 16, 2013 at 8:44
  • In order for [System.Convert]::ToBoolean($Unify) to evaluate to true, $Unify must -eq "true" or "True". Otherwise, [System.Convert]::ToBoolean($Unify) evaluates to false. Commented Feb 14, 2021 at 0:16
20

This is an older question, but there is actually an answer to this in the PowerShell documentation. I had the same problem, and for once RTFM actually solved it. Almost.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe

Documentation for the -File parameter states that "In rare cases, you might need to provide a Boolean value for a switch parameter. To provide a Boolean value for a switch parameter in the value of the File parameter, enclose the parameter name and value in curly braces, such as the following: -File .\Get-Script.ps1 {-All:$False}"

I had to write it like this:

PowerShell.Exe -File MyFile.ps1 {-SomeBoolParameter:False}

So no '$' before the true/false statement, and that worked for me, on PowerShell 4.0

4
  • 1
    Thank you for sharing! Solved my problem perfectly. Commented Jul 31, 2018 at 0:56
  • 1
    The link in the answer seems to have had its content changed. However, I found the answer here Commented Sep 7, 2018 at 18:08
  • Current documentation link about_powershell.exe alternatively just rung powershell -h.
    – Seth
    Commented Oct 17, 2018 at 7:40
  • 3
    I'm surprised that this awkward workaround ever worked - it doesn't in Windows PowerShell v5.1 (neither with nor without a leading $); also note that it is no longer necessary in PowerShell Core. I've asked for the documentation to be corrected; see github.com/MicrosoftDocs/PowerShell-Docs/issues/4964
    – mklement0
    Commented Oct 19, 2019 at 13:56
19

Try setting the type of your parameter to [bool]:

param
(
    [int]$Turn = 0
    [bool]$Unity = $false
)

switch ($Unity)
{
    $true { "That was true."; break }
    default { "Whatever it was, it wasn't true."; break }
}

This example defaults $Unity to $false if no input is provided.

Usage

.\RunScript.ps1 -Turn 1 -Unity $false
2
  • 1
    Emperor XLII's answer and your comment on it regarding the -File parameter should cover every aspect of the original question. I'll leave my answer because someone may also find my hint on providing a default value useful.
    – Filburt
    Commented Apr 16, 2013 at 19:24
  • Well done Filburt. Your answer caused me to check my script, which already has default specified as the required value $false (I wrote that several years ago and had forgotten all about it) - so I don't even have to specify the problematic boolean parameter on the command line now. Excellent :)
    – Zeek2
    Commented Mar 29, 2018 at 7:39
7

I think, best way to use/set boolean value as parameter is to use in your PS script it like this:

Param(
    [Parameter(Mandatory=$false)][ValidateSet("true", "false")][string]$deployApp="false"   
)

$deployAppBool = $false
switch($deployPmCmParse.ToLower()) {
    "true" { $deployAppBool = $true }
    default { $deployAppBool = $false }
}

So now you can use it like this:

.\myApp.ps1 -deployAppBool True
.\myApp.ps1 -deployAppBool TRUE
.\myApp.ps1 -deployAppBool true
.\myApp.ps1 -deployAppBool "true"
.\myApp.ps1 -deployAppBool false
#and etc...

So in arguments from cmd you can pass boolean value as simple string :).

6

Running powershell scripts on linux from bash gives the same problem. Solved it almost the same as LarsWA's answer:

Working:

pwsh -f ./test.ps1 -bool:true

Not working:

pwsh -f ./test.ps1 -bool=1
pwsh -f ./test.ps1 -bool=true
pwsh -f ./test.ps1 -bool true
pwsh -f ./test.ps1 {-bool=true}
pwsh -f ./test.ps1 -bool=$true
pwsh -f ./test.ps1 -bool=\$true
pwsh -f ./test.ps1 -bool 1
pwsh -f ./test.ps1 -bool:1
2
  • 4
    same situation here. This is dumb.
    – dgo
    Commented May 6, 2021 at 22:01
  • oh no it was just one : i missedd
    – Yeonghun
    Commented Mar 3, 2023 at 18:37
6

To summarize and complement the existing answers, as of Windows PowerShell v5.1 (the latest and last version) / PowerShell (Core) 7.3.3:

David Mohundro's answer rightfully points that instead of [bool] parameters you should use [switch] parameters in PowerShell, where the presence vs. absence of the switch name (-Unify specified vs. not specified) implies its value, which makes the original problem go away.


However, on occasion you may still need to pass the switch value explicitly, particularly if you're constructing a command line programmatically:


In PowerShell Core 7+, the original problem (described in Emperor XLII's answer) has been fixed.

That is, to pass $true explicitly to a [switch] parameter named -Unify you can now write:

pwsh -File .\RunScript.ps1 -Unify:$true  # !! ":" separates name and value, no space

The following values can be used: $false, false, $true, true, but note that passing 0 or 1 does not work.

Note how the switch name is separated from the value with : and there must be no whitespace between the two.

Note: If you declare a [bool] parameter instead of a [switch] (which you generally shouldn't), you must use the same syntax. Even though -Unify $false should work, it currently doesn't - see GitHub issue #10838.


In Windows PowerShell, the original problem persists, and - given that Windows PowerShell is no longer actively developed - is unlikely to get fixed.

:: # From cmd.exe
powershell -Command "& .\RunScript.ps1 -Unify:$true" 

With -Command you're effectively passing a piece of PowerShell code, which is then evaluated as usual - and inside PowerShell passing $true and $false works (but not true and false, as now also accepted with -File).

Caveats:

  • Using -Command can result in additional interpretation of your arguments, such as if they contain $ chars. (with -File, arguments are literals).

  • Using -Command can result in a different exit code.

For details, see this answer and this answer.

2
  • 1
    The most complete answer. This one should be marked as an answer here
    – Kamarey
    Commented Mar 14, 2023 at 13:23
  • I appreciate the sentiment, @Kamarey. It's unlikely that the OP will come back and change what answer is the accepted one, but up-voting this one (which I assume you have done, thank you) will help it become more visible.
    – mklement0
    Commented Mar 14, 2023 at 13:42
3

In PowerShell, boolean parameters can be declared by mentioning their type before their variable.

    function GetWeb() {
             param([bool] $includeTags)
    ........
    ........
    }

You can assign value by passing $true | $false

    GetWeb -includeTags $true
2
  • Just passing the named input arguments explicitly like that worked a treat. Thanks Ahmed Commented Feb 20, 2020 at 12:02
  • The question is about calling the PowerShell CLI (powershell.exe) from cmd.exe, invoking a script that already has as [bool] parameter defined. You're answering an unrelated question.
    – mklement0
    Commented Mar 14, 2023 at 13:33
0

I had something similar when passing a script to a function with invoke-command. I ran the command in single quotes instead of double quotes, because it then becomes a string literal. 'Set-Mailbox $sourceUser -LitigationHoldEnabled $false -ElcProcessingDisabled $true';

-1

Based on my test, a default value specification does not work for a parameter that is expected to be a boolean in the Function.

You can use the [switch] type or if you really want to use boolean, you should specify default value at the start of the function like below:

Function nullRules {
    Param
    (
         [Parameter(Mandatory=$false)] $boolWins

    )
   if ([DBNull]::Value.Equals($boolWins)) {$boolWins=$false}
   ....
   ....
}
1
  • Based on mt testing, ([DBNull]::Value.Equals($boolWins)) returns $false whether a value is passed to $boolWins or not. Therefore, the call to set $boolWins to $false if $boolWins is $null is superfluous. If $boolWins is omitted then a default boolean value of $false is automatically assigned.
    – Guru Josh
    Commented Mar 7, 2023 at 14:05
-4

You can also use 0 for False or 1 for True. It actually suggests that in the error message:

Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.

For more info, check out this MSDN article on Boolean Values and Operators.

2
  • 11
    That does not work. It is expecting integer number 1 or 0, but passing it through -File interprets it as string, thus fails with same error. Commented Apr 20, 2015 at 9:10
  • This answer is wrong, as the suggestion doesn't work
    – mjs
    Commented Apr 28, 2016 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.