Powershell

How to Pass Boolean Values to a PowerShell Script From a Command Prompt

PowerShell also has a host application, “PowerShell ISE”, that is used to create script files. A PowerShell script is like a text file that includes multiple commands inside it with a “.ps1” extension. This script can be modified using multiple “CLI(Command Line Interface)” tools, including “Command Prompt”. A CMD(Command Prompt) can help pass values to a PowerShell script using specific commands.

This write-up will provide a complete guide to pass Boolean values to a PowerShell script

How to Pass Boolean Values to a PowerShell Script from a Command Prompt?

Boolean is a data type used to represent one of two possible Boolean values: “True” or “False”. These values are usually used in conditional statements. More specifically, you can pass these Boolean values to a PowerShell script with the help of the Command Prompt.

Example

In this example, we will pass the Boolean value to a PowerShell script using Command Prompt:

param($x, $y)
switch($x){
$true {"Output is True."}
$false {"Output is false."}
}

 

According to the given code:

  • First, define a “param()” method and then pass the “$x” and “$y” parameters inside it.
  • After that, add the “switch” statement and pass the “$x” variable as a parameter.
  • Inside the switch statement, allocate the stated messages to the specified Boolean values:

The PowerShell script has been created successfully. Now, let’s pass the value to it using “Command Prompt”.

Passing “True” Boolean Value to a PowerShell Script

Let’s first pass the “$True” Boolean value to the parameter:

> powershell.exe C:\Doc\Bool.ps1 -x $True -y 1

 

The value has been passed to the script and the string assigned to the “$true” variable has been displayed.

Passing “False” Boolean Value to a PowerShell Script

Now, let’s pass the “$False” value to the parameter “$x”:

> powershell.exe C:\Doc\Bool.ps1 -x $False -y 0

 

It can be observed that the value assigned to the “$False” variable has printed out on console and it confirms that the value was passed to the PowerShell script.

Conclusion

The Boolean values can be passed to a PowerShell script from “Command Prompt”. In order to do so, first, launch “CMD”. Then, write “powershell.exe” first followed by the script file path, write the argument “-x” and “-y”, and assign the Boolean values to them. This write-up provided a detailed guide to pass the Boolean values to a PowerShell script.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.