Skip to content

Commit 42bee23

Browse files
authored
Merge pull request #12073 from MicrosoftDocs/main
5/15/2025 PM Publish
2 parents 8afe827 + 25cd8aa commit 42bee23

File tree

4 files changed

+252
-120
lines changed

4 files changed

+252
-120
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to use a switch to handle multiple `if` statements.
33
Locale: en-US
4-
ms.date: 02/19/2025
4+
ms.date: 05/15/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Switch
@@ -10,35 +10,34 @@ title: about_Switch
1010

1111
## Short description
1212

13-
Explains how to use a switch to handle multiple `if` statements.
13+
Explains how to use a switch to handle multiple conditional statements.
1414

1515
## Long description
1616

17-
To check a condition in a script or function, use an `if` statement. The `if`
18-
statement can check many types of conditions, including the value of variables
19-
and the properties of objects.
17+
To check a condition in a script or function, you can use an `if` statement.
18+
The `if` statement can check many types of conditions, including the value of
19+
variables and the properties of objects.
2020

21-
To check multiple conditions, use a `switch` statement. The `switch` statement
22-
is equivalent to a series of `if` statements, but it's simpler. The `switch`
23-
statement lists each condition and an optional action. If a condition obtains,
24-
the action is performed.
21+
To check multiple conditions, you can use a `switch` statement. The `switch`
22+
statement is similar to a series of `if` statements, but it's simpler. The
23+
`switch` statement lists each condition and an optional action. If a condition
24+
matches, the action is performed.
2525

2626
The `switch` statement can use the `$_` and `$switch` automatic variables. For
27-
more information, see [about_Automatic_Variables][02].
27+
more information, see [about_Automatic_Variables][03].
2828

2929
## Syntax
3030

3131
A basic `switch` statement has the following format:
3232

3333
```Syntax
34-
switch (<test-expression>)
35-
{
34+
switch (<test-expression>) {
3635
<result1-to-be-matched> {<action>}
3736
<result2-to-be-matched> {<action>}
3837
}
3938
```
4039

41-
The equivalent `if` statements are:
40+
This is similar to the following `if` statements:
4241

4342
```Syntax
4443
if (<result1-to-be-matched> -eq (<test-expression>)) {<action>}
@@ -50,18 +49,14 @@ mode to return a value.
5049

5150
The `<result-to-be-matched>` is an expression whose value is compared to the
5251
input value. Expressions include literal values (strings or numbers),
53-
variables, and scriptblocks that return a boolean value.
54-
55-
Any unquoted value that's not recognized as a number is treated as a string.
56-
To avoid confusion or unintended string conversion, you should always quote
57-
string values. Enclose any expressions in parentheses `()`, creating
58-
subexpressions, to ensure that the expression is evaluated correctly.
52+
variables, and scriptblocks that return a boolean value. For an example, see
53+
[Impact of string conversion][02] later in this article.
5954

6055
It's important to understand that the `<result-to-be-matched>` value is on the
6156
left-hand side of the comparison expression. That means the result of the
6257
`<test-expression>` is on the right-hand side, which can be converted to the
6358
type of the left-hand side value for comparison. For more information, see
64-
[about_Comparison_Operators][04]
59+
[about_Comparison_Operators][05]
6560

6661
The value `default` is reserved for the action used when there are no other
6762
matches.
@@ -218,6 +213,43 @@ switch (4, 2) {
218213
It's four.
219214
```
220215

216+
## Impact of string conversion
217+
218+
Any unquoted value that's not recognized as a number is treated as a string. To
219+
avoid unintended string conversion, use script blocks to evaluate the switch
220+
value.
221+
222+
```powershell
223+
switch ( ([datetime]'1 Jan 1970').DayOfWeek ) {
224+
4 { 'The value matches a Thursday.' }
225+
"4" { 'The numeric string matches a Thursday.' }
226+
"Thursday" { 'The string value matches a Thursday.' }
227+
{ 4 -eq $_ } { 'The expression matches a Thursday.' }
228+
}
229+
```
230+
231+
The **DayOfWeek** property of the date object is an enumeration. While
232+
enumerations can be compared to their numeric or string values, the `switch`
233+
statement converts the value to a the string representation of the enumeration.
234+
235+
```Output
236+
The string value matches a Thursday.
237+
The expression matches a Thursday.
238+
```
239+
240+
This behavior is different from the behavior of the `-eq` comparison in an `if`
241+
statement.
242+
243+
```powershell
244+
if (4 -eq ([datetime]'1 Jan 1970').DayOfWeek) {
245+
'The value matches a Thursday.'
246+
}
247+
```
248+
249+
```Output
250+
The value matches a Thursday.
251+
```
252+
221253
## More complex match examples
222254

223255
In this example, the `switch` statement is testing for the type of the value in
@@ -445,16 +477,17 @@ switch -File $fileEscaped { foo { 'Foo' } }
445477

446478
## See also
447479

448-
- [about_Break][03]
449-
- [about_Continue][05]
450-
- [about_If][06]
451-
- [about_Script_Blocks][07]
480+
- [about_Break][04]
481+
- [about_Continue][06]
482+
- [about_If][07]
483+
- [about_Script_Blocks][08]
452484

453485
<!-- updated link references -->
454486
[01]: #file-parameter-examples
455-
[02]: about_Automatic_Variables.md
456-
[03]: about_break.md
457-
[04]: about_Comparison_Operators.md
458-
[05]: about_Continue.md
459-
[06]: about_If.md
460-
[07]: about_Script_Blocks.md
487+
[02]: #impact-of-string-conversion
488+
[03]: about_Automatic_Variables.md
489+
[04]: about_break.md
490+
[05]: about_Comparison_Operators.md
491+
[06]: about_Continue.md
492+
[07]: about_If.md
493+
[08]: about_Script_Blocks.md

reference/7.4/Microsoft.PowerShell.Core/About/about_Switch.md

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to use a switch to handle multiple `if` statements.
33
Locale: en-US
4-
ms.date: 02/19/2025
4+
ms.date: 05/15/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Switch
@@ -10,35 +10,34 @@ title: about_Switch
1010

1111
## Short description
1212

13-
Explains how to use a switch to handle multiple `if` statements.
13+
Explains how to use a switch to handle multiple conditional statements.
1414

1515
## Long description
1616

17-
To check a condition in a script or function, use an `if` statement. The `if`
18-
statement can check many types of conditions, including the value of variables
19-
and the properties of objects.
17+
To check a condition in a script or function, you can use an `if` statement.
18+
The `if` statement can check many types of conditions, including the value of
19+
variables and the properties of objects.
2020

21-
To check multiple conditions, use a `switch` statement. The `switch` statement
22-
is equivalent to a series of `if` statements, but it's simpler. The `switch`
23-
statement lists each condition and an optional action. If a condition obtains,
24-
the action is performed.
21+
To check multiple conditions, you can use a `switch` statement. The `switch`
22+
statement is similar to a series of `if` statements, but it's simpler. The
23+
`switch` statement lists each condition and an optional action. If a condition
24+
matches, the action is performed.
2525

2626
The `switch` statement can use the `$_` and `$switch` automatic variables. For
27-
more information, see [about_Automatic_Variables][02].
27+
more information, see [about_Automatic_Variables][03].
2828

2929
## Syntax
3030

3131
A basic `switch` statement has the following format:
3232

3333
```Syntax
34-
switch (<test-expression>)
35-
{
34+
switch (<test-expression>) {
3635
<result1-to-be-matched> {<action>}
3736
<result2-to-be-matched> {<action>}
3837
}
3938
```
4039

41-
The equivalent `if` statements are:
40+
This is similar to the following `if` statements:
4241

4342
```Syntax
4443
if (<result1-to-be-matched> -eq (<test-expression>)) {<action>}
@@ -50,18 +49,14 @@ mode to return a value.
5049

5150
The `<result-to-be-matched>` is an expression whose value is compared to the
5251
input value. Expressions include literal values (strings or numbers),
53-
variables, and scriptblocks that return a boolean value.
54-
55-
Any unquoted value that's not recognized as a number is treated as a string.
56-
To avoid confusion or unintended string conversion, you should always quote
57-
string values. Enclose any expressions in parentheses `()`, creating
58-
subexpressions, to ensure that the expression is evaluated correctly.
52+
variables, and scriptblocks that return a boolean value. For an example, see
53+
[Impact of string conversion][02] later in this article.
5954

6055
It's important to understand that the `<result-to-be-matched>` value is on the
6156
left-hand side of the comparison expression. That means the result of the
6257
`<test-expression>` is on the right-hand side, which can be converted to the
6358
type of the left-hand side value for comparison. For more information, see
64-
[about_Comparison_Operators][04]
59+
[about_Comparison_Operators][05]
6560

6661
The value `default` is reserved for the action used when there are no other
6762
matches.
@@ -218,6 +213,43 @@ switch (4, 2) {
218213
It's four.
219214
```
220215

216+
## Impact of string conversion
217+
218+
Any unquoted value that's not recognized as a number is treated as a string. To
219+
avoid unintended string conversion, use script blocks to evaluate the switch
220+
value.
221+
222+
```powershell
223+
switch ( ([datetime]'1 Jan 1970').DayOfWeek ) {
224+
4 { 'The value matches a Thursday.' }
225+
"4" { 'The numeric string matches a Thursday.' }
226+
"Thursday" { 'The string value matches a Thursday.' }
227+
{ 4 -eq $_ } { 'The expression matches a Thursday.' }
228+
}
229+
```
230+
231+
The **DayOfWeek** property of the date object is an enumeration. While
232+
enumerations can be compared to their numeric or string values, the `switch`
233+
statement converts the value to a the string representation of the enumeration.
234+
235+
```Output
236+
The string value matches a Thursday.
237+
The expression matches a Thursday.
238+
```
239+
240+
This behavior is different from the behavior of the `-eq` comparison in an `if`
241+
statement.
242+
243+
```powershell
244+
if (4 -eq ([datetime]'1 Jan 1970').DayOfWeek) {
245+
'The value matches a Thursday.'
246+
}
247+
```
248+
249+
```Output
250+
The value matches a Thursday.
251+
```
252+
221253
## More complex match examples
222254

223255
In this example, the `switch` statement is testing for the type of the value in
@@ -445,16 +477,17 @@ switch -File $fileEscaped { foo { 'Foo' } }
445477

446478
## See also
447479

448-
- [about_Break][03]
449-
- [about_Continue][05]
450-
- [about_If][06]
451-
- [about_Script_Blocks][07]
480+
- [about_Break][04]
481+
- [about_Continue][06]
482+
- [about_If][07]
483+
- [about_Script_Blocks][08]
452484

453485
<!-- updated link references -->
454486
[01]: #file-parameter-examples
455-
[02]: about_Automatic_Variables.md
456-
[03]: about_break.md
457-
[04]: about_Comparison_Operators.md
458-
[05]: about_Continue.md
459-
[06]: about_If.md
460-
[07]: about_Script_Blocks.md
487+
[02]: #impact-of-string-conversion
488+
[03]: about_Automatic_Variables.md
489+
[04]: about_break.md
490+
[05]: about_Comparison_Operators.md
491+
[06]: about_Continue.md
492+
[07]: about_If.md
493+
[08]: about_Script_Blocks.md

0 commit comments

Comments
 (0)