21

When I study PowerShell scripting language, I knew the data type is auto-assignment. If I want to declare a Boolean type variable, how can I do it?

Example: $myvariable = "string" or $myvalue = 3.14159 $myboolean ?

karel
  • 5,489
  • 46
  • 45
  • 50
HushChen
  • 345
  • 1
  • 2
  • 5

1 Answers1

45

Boolean values (which can be either 1 or 0) are defined in PowerShell using the .Net System.Boolean type (the short from of which is [bool]). For example, the following command assigns true to a variable of boolean type:

PS C:\Users\Administrator> [bool] $myval = 1
PS C:\Users\Administrator> $myval.gettype().fullname
System.Boolean

When working with boolean variables, the pre-defined

$true

and

$false

variables may also be used:

PS C:\Users\Administrator> [bool] $myval = $false
PS C:\Users\Administrator> $myval
False
Akash Pal
  • 1,055
  • 8
  • 15