What does the special character !
mean in PowerShell?
Or a site which lists all special characters and meaning. Example:
$string = blah
!$String
(Returns $false)
What does the special character !
mean in PowerShell?
Or a site which lists all special characters and meaning. Example:
$string = blah
!$String
(Returns $false)
PowerShell uses the !
character as an alias for the logical -not
operator:
$true
!$true
$false
!$false
True
False
False
True
PowerShell interprets everything that is empty, $Null, or 0 to the Boolean $False. Bool can only have $True or $False.
By casting the value to a Boolean you can see what PowerShell interprets for each value:
[bool]0 # False
[bool]1 # True
[bool]"" # False
[bool]"test" # True
[bool]$null # False
The locical NOT operation turns each Boolean into its opposite:
!$True # Is $False
!$False # Is $True
![bool]0 # True
![bool]1 # False
![bool]"" # True
![bool]"test" # False
![bool]$null # True
You were assigning a string to a variable and then checking whether it is empty or not.
$string = blah
!$String # $String is not $Null or Empty so it is $True
# But the !(NOT) operation turns it to $False
Conditionals and loops in programming languages only work with Boolean values.
So when getting user input you can use this to check whether the user has input text, or not, and react on it:
$UserName = Read-Host -Prompt "Whats your Name Sir?"
If ($UserName) {
Write-Output "Happy Birthday $UserName"
}
Else {
Write-Output "I can't congratulate you as I don't know your name :("
}
The !
(exclamation mark) character in PowerShell is a shortcut to the -not
operator ('not equal').
For example:
$a = $null;
if(!$a) {
Write-Host '$a is null'
}
$a is null
The symbol !
is an alias for -Not
, which is a unary (one-argument) operator that casts its argument to a Boolean value and then returns the logical opposite of that value. (Most of the time, spelling out -Not
is a better choice for readability.)
Boolean is a data type with only two possible values: true and false. In PowerShell the type is known as Bool
and the two values are written $True
and $False
. The -Not
/!
operator just flips the value to its opposite: -Not $True
is $False
and ! $False
is $True
.
But in PowerShell, as in many other programming languages, you can apply Boolean operators (which also include -And
and -Or
) to non-Boolean values; those values just get automatically cast to Bool
before being operated on, as if you had put a [Bool]
in front of them.
Values that become $True
when so cast are called "truthy"; values that become $False
are called "falsy" (sometimes spelled "falsey"). Different programming languages have different conventions for what goes in each category; for instance, an empty array is falsy in Perl and Python, but truthy in Ruby.
In PowerShell, both the null value $Null
and the empty string are falsy. So in your code example, !$String
would be true if the string is either null or empty (since it would boolify to false and then get flipped to true by the !
). Such an expression would likely show up in, for example, an If
statement that sets a variable to a default value if it has not already been set to a different one by earlier code.
Besides the empty string and $Null
, other falsy values in PowerShell include the number 0 and the empty array. However, an empty HashTable is truthy. You can see these correspondences by casting the values to Boolean, either with [Bool]
or the shortcut !!
. (!
casts the value to Boolean but then flips it; the second !
flips it back. I recommend using !!
at the prompt for quicker typing, but spelling out [Bool]
for better self-documentation in production code.) Here are some examples:
PS /> [Bool]$Null
False
PS /> !!''
False
PS /> !!0
False
PS /> !!@()
False
PS /> !!@{}
True
© 2022 - 2024 — McMap. All rights reserved.