$UserChoice = Read-Host "Enter # of tool you want to run"
switch -exact ($UserChoice) {
1 {Write-Host 'You selected 1'}
1a {Write-Host 'You selected 1a'}
1b {Write-Host 'You selected 1b'}
1c {Write-Host 'You selected 1c'}
1d {Write-Host 'You selected 1d'}
}
Preface: I know the fix is to put the comparison values in quotes, ex: '1d'. I want to know WHY PowerShell is acting the way it is for learning sake.
I noticed VISUALLY the comparison values are different colors (1 & 1d are darker) so I guess PowerShell ISE syntax highlighting is telling us they're being treated differently. 4. What do the colors mean (maroon/dark-RedOrPurple VS purple?

Before explaining why the 1d label is "special", I should note that the -exact mode (which is the default mode of comparison for a switch statement) is probably a bit misleading.
It simply means "use the -eq operator to compare input values to case labels".
The reason 1d behaves differently is that PowerShell doesn't recognize the expression 1d as a string. Instead, it interprets d is a numerical suffix signifying the [decimal] type, and the case label value is thus the same as if you'd written 1.0 or $([decimal]1).
The result is that comparison to the input string "1" comes out the same for both - "1" -eq 1 and "1" -eq 1d are both true, thanks to PowerShell's overloaded operators.
If you ever expand your options further, you'll encounter the same problem with 1l (l = [long]), and, if using PowerShell 7, eventually 1n, 1s, 1u, and 1y.
Quote the switch labels to avoid PowerShell parsing them as a numerical expressions:
$UserChoice = Read-Host "Enter # of tool you want to run"
switch -exact ($UserChoice) {
'1' {Write-Host 'You selected 1'}
'1a' {Write-Host 'You selected 1a'}
'1b' {Write-Host 'You selected 1b'}
'1c' {Write-Host 'You selected 1c'}
'1d' {Write-Host 'You selected 1d'}
}
See the about_Numeric_Literals help topic for a more comprehensive overview of suffixes interpreted as numerical modfiers by PowerShell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With