How to declare a variable and its type is Boolean in PowerShell? [duplicate]

依然范特西╮ 提交于 2021-01-02 06:30:20

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/54721256/how-to-declare-a-variable-and-its-type-is-boolean-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!