[ad_1]
To add more information to already existing answers: The Boolean literals $true
and $false
also work as is when used as command line parameters for PowerShell scripts. For the below PowerShell script which is stored in a file named installmyapp.ps1
:
param (
[bool]$cleanuprequired
)
echo "Batch file starting execution."
Now if I’ve to invoke this PowerShell file from a PowerShell command line, this is how I can do it:
installmyapp.ps1 -cleanuprequired $true
OR
installmyapp.ps1 -cleanuprequired 1
Here 1
and $true
are equivalent. Also, 0
and $false
are equivalent.
Note: Never expect that string literal true
can get automatically converted to boolean. For example, if I run the below command:
installmyapp.ps1 -cleanuprequired true
it fails to execute the script with the below error:
Cannot process argument transformation on parameter ‘cleanuprequired’.
Cannot convert value “System.String” to type “System.Boolean”. Boolean
parameters accept only Boolean values and numbers, such as $True,
$False, 1 or 0.
[ad_2]