11---
22description : Explains how to use a switch to handle multiple `if` statements.
33Locale : en-US
4- ms.date : 05/15 /2025
4+ ms.date : 05/19 /2025
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about_Switch
@@ -20,11 +20,11 @@ variables and the properties of objects.
2020
2121To check multiple conditions, you can use a ` switch ` statement. The ` switch `
2222statement 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.
23+ ` switch ` statement lists each condition and the corresponding action. If a
24+ condition matches, the action is performed.
2525
26- The ` switch ` statement can use the ` $_ ` and ` $switch ` automatic variables. For
27- more information, see [ about_Automatic_Variables ] [ 03 ] .
26+ > [ !IMPORTANT ]
27+ > The ` switch ` statement converts all values to strings before comparison .
2828
2929## Syntax
3030
@@ -37,33 +37,33 @@ switch (<test-expression>) {
3737}
3838```
3939
40- This is similar to the following ` if ` statements:
40+ The syntax of a ` switch ` statement is similar to the following ` if ` statements:
4141
4242``` Syntax
43- if (<result1-to-be-matched> -eq (<test-expression>)) {<action>}
44- if (<result2-to-be-matched> -eq (<test-expression>)) {<action>}
43+ if ("$( <result1-to-be-matched>)") -eq ("$( <test-expression>)" ) {<action>}
44+ if ("$( <result2-to-be-matched>)") -eq ("$( <test-expression>)" ) {<action>}
4545```
4646
47- The ` <test-expression> ` is single expression that's evaluated in expression
48- mode to return a value.
49-
50- The ` <result-to-be-matched> ` is an expression whose value is compared to the
51- input value. Expressions include literal values (strings or numbers),
52- variables, and scriptblocks that return a boolean value. For an example, see
47+ Expressions include literal values (strings or numbers), variables, and
48+ scriptblocks that return a boolean value. The ` switch ` statement converts all
49+ values to strings before comparison. For an example, see
5350[ Impact of string conversion] [ 02 ] later in this article.
5451
55- It's important to understand that the ` <result-to-be-matched> ` value is on the
56- left-hand side of the comparison expression. That means the result of the
57- ` <test-expression> ` is on the right-hand side, which can be converted to the
58- type of the left-hand side value for comparison. For more information, see
59- [ about_Comparison_Operators] [ 05 ]
52+ The ` <test-expression> ` is evaluated in expression mode. If the expression
53+ returns more than one value, such as an array or other enumerable type, the
54+ ` switch ` statement evaluates each enumerated value separately.
55+
56+ The ` <result-to-be-matched> ` is an expression must resolve to a single value.
57+ That value is compared to the input value.
6058
6159The value ` default ` is reserved for the action used when there are no other
6260matches.
6361
64- The ` $_ ` automatic variable contains the value of the expression passed to the
65- ` switch ` statement and is available for evaluation and use within the scope of
66- the ` <result-to-be-matched> ` statements.
62+ The ` switch ` statement can use the ` $_ ` and ` $switch ` automatic variables. The
63+ automatic variable contains the value of the expression passed to the ` switch `
64+ statement and is available for evaluation and use within the scope of the
65+ ` <result-to-be-matched> ` statements. For more information, see
66+ [ about_Automatic_Variables] [ 03 ] .
6767
6868The complete ` switch ` statement syntax is as follows:
6969
@@ -109,7 +109,8 @@ conditions. It's equivalent to an `else` clause in an `if` statement. Only one
109109 file is read a line at a time and evaluated by the ` switch ` statement. By
110110 default, the comparison is case-insensitive. The ** File** parameter only
111111 supports one file. If multiple ** File** parameters are included, only the
112- last one is used. For more information see [ File parameter examples] [ 01 ] .
112+ last one is used. For more information see the
113+ [ ** File** parameter examples] [ 01 ] .
113114- ** Regex** - Performs regular expression matching of the value to the
114115 condition. If the match clause isn't a string, this parameter is ignored.
115116 The comparison is case-insensitive. The ` $Matches ` automatic variable is
@@ -121,7 +122,11 @@ conditions. It's equivalent to an `else` clause in an `if` statement. Only one
121122> ignored. Multiple instances of parameters are also permitted. However, only
122123> the last parameter listed is used.
123124
124- ## Simple match examples
125+ ## Examples
126+
127+ The following examples demonstrate the use of the ` switch ` statement.
128+
129+ ### Simple match examples
125130
126131In the following example, the ` switch ` statement compares the test value ` 3 ` to
127132each of the conditions. When the test value matches the condition, the action
@@ -159,69 +164,44 @@ It's three.
159164Three again.
160165```
161166
162- To direct the ` switch ` to stop comparing after a match, use the ` break `
163- statement. The ` break ` statement terminates the ` switch ` statement.
164-
165- ``` powershell
166- switch (3) {
167- 1 { "It's one." }
168- 2 { "It's two." }
169- 3 { "It's three."; break }
170- 4 { "It's four." }
171- 3 { "Three again." }
172- }
173- ```
174-
175- ``` Output
176- It's three.
177- ```
167+ ### Use ` break ` and ` continue ` to control the flow
178168
179- If the test value is a collection, such as an array, each item in the
180- collection is evaluated in the order in which it appears. The following
181- examples evaluates 4 and then 2.
169+ If the value matches multiple conditions, the action for each condition is
170+ executed. To change this behavior, use the ` break ` or ` continue ` keywords.
182171
183- ``` powershell
184- switch (4, 2) {
185- 1 { "It's one." }
186- 2 { "It's two." }
187- 3 { "It's three." }
188- 4 { "It's four." }
189- 3 { "Three again." }
190- }
191- ```
172+ The ` break ` keyword stops processing and exits the ` switch ` statement.
192173
193- ``` Output
194- It's four.
195- It's two.
196- ```
174+ The ` continue ` keyword stops processing the current value, but continues
175+ processing any subsequent values.
197176
198- Any ` break ` statements apply to the collection, not to each value, as shown
199- in the following example. The ` switch ` statement is terminated by the ` break `
200- statement in the condition of value 4 .
177+ The following example processes an array of numbers and displays if they're
178+ odd or even. Negative numbers are skipped with the ` continue ` keyword. If a
179+ non-number is encountered, execution is terminated with the ` break ` keyword .
201180
202181``` powershell
203- switch (4, 2) {
204- 1 { "It's one."; break }
205- 2 { "It's two." ; break }
206- 3 { "It's three." ; break }
207- 4 { "It's four." ; break }
208- 3 { "Three again." }
182+ switch (1,4,-1,3,"Hello",2,1) {
183+ {$_ -lt 0} { continue }
184+ {$_ -isnot [int32]} { break }
185+ {$_ % 2} { "$_ is Odd" }
186+ {-not ($_ % 2)} { "$_ is Even" }
209187}
210188```
211189
212190``` Output
213- It's four.
191+ 1 is Odd
192+ 4 is Even
193+ 3 is Odd
214194```
215195
216- ## Impact of string conversion
196+ ### Impact of string conversion
217197
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.
198+ All values, both input and the comparison value are converted to strings for
199+ comparison. To avoid unintended string conversion, use script blocks to
200+ evaluate the switch value.
221201
222202``` powershell
223203switch ( ([datetime]'1 Jan 1970').DayOfWeek ) {
224- 4 { 'The value matches a Thursday.' }
204+ 4 { 'The integer value matches a Thursday.' }
225205 "4" { 'The numeric string matches a Thursday.' }
226206 "Thursday" { 'The string value matches a Thursday.' }
227207 { 4 -eq $_ } { 'The expression matches a Thursday.' }
@@ -242,59 +222,69 @@ statement.
242222
243223``` powershell
244224if (4 -eq ([datetime]'1 Jan 1970').DayOfWeek) {
245- 'The value matches a Thursday.'
225+ 'The integer value matches a Thursday.'
246226}
247227```
248228
249229``` Output
250230The value matches a Thursday.
251231```
252232
253- ## More complex match examples
254-
255- In this example, the ` switch ` statement is testing for the type of the value in
256- the hashtable. You must use and expression that returns a boolean value to
257- select the scriptblock to execute.
233+ In this example, a hashtable is passed to the ` switch ` statement. The ` switch `
234+ converts the hashtable to a string.
258235
259236``` powershell
260- $var = @{A = 10; B = 'abc'}
261-
262- foreach ($key in $var.Keys) {
263- switch ($var[$key].GetType()) {
264- { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
265- { $_ -eq [string] } { "$key = $($var[$key])" }
266- }
237+ $test = @{
238+ Test = 'test'
239+ Test2 = 'test2'
267240}
241+
242+ $test.ToString()
268243```
269244
270245``` Output
271- A + 10 = 20
272- B = abc
246+ System.Collections.Hashtable
273247```
274248
275- In this example, an object that's not a string or numerical data is passed to
276- the ` switch ` . The ` switch ` performs a string coercion on the object and
277- evaluates the outcome.
249+ Notice that the string representation of the hashtable is not the same as the
250+ value of the ** Test** key.
278251
279252``` powershell
280- $test = @{
281- Test = 'test'
282- Test2 = 'test2'
283- }
284-
285- $test.ToString()
286-
287253switch -Exact ($test) {
288254 'System.Collections.Hashtable' { 'Hashtable string coercion' }
289255 'test' { 'Hashtable value' }
290256}
291257```
292258
293259``` Output
294- System.Collections.Hashtable
295260Hashtable string coercion
296261```
297262
263+ ### Use ` switch ` to test the values in a hashtable
264+
265+ In this example, the ` switch ` statement is testing for the type of the value in
266+ the hashtable. We must enumerate the items in the hashtable before we can test
267+ the values. To avoid the complications of string conversion, use a script block
268+ that returns a boolean value to select the action scriptblock to execute.
269+
270+ ``` powershell
271+ $var = @{A = 10; B = 'abc'}
272+
273+ foreach ($key in $var.Keys) {
274+ switch ($var[$key].GetType()) {
275+ { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
276+ { $_ -eq [string] } { "$key = $($var[$key])" }
277+ }
278+ }
279+ ```
280+
281+ ``` Output
282+ A + 10 = 20
283+ B = abc
284+ ```
285+
286+ ### Use wildcards with ` switch `
287+
298288In this example, there is no matching case so there is no output.
299289
300290``` powershell
@@ -342,6 +332,8 @@ switch -Wildcard ("fourteen") {
342332That's too many.
343333```
344334
335+ ### Use regular expressions with ` switch `
336+
345337The following example uses the ` -Regex ` parameter.
346338
347339``` powershell
@@ -400,34 +392,7 @@ switch ((Get-Date 1-Jan-2022), (Get-Date 25-Dec-2021)) {
400392}
401393```
402394
403- If the value matches multiple conditions, the action for each condition is
404- executed. To change this behavior, use the ` break ` or ` continue ` keywords.
405-
406- The ` break ` keyword stops processing and exits the ` switch ` statement.
407-
408- The ` continue ` keyword stops processing the current value, but continues
409- processing any subsequent values.
410-
411- The following example processes an array of numbers and displays if they're
412- odd or even. Negative numbers are skipped with the ` continue ` keyword. If a
413- non-number is encountered, execution is terminated with the ` break ` keyword.
414-
415- ``` powershell
416- switch (1,4,-1,3,"Hello",2,1) {
417- {$_ -lt 0} { continue }
418- {$_ -isnot [int32]} { break }
419- {$_ % 2} { "$_ is Odd" }
420- {-not ($_ % 2)} { "$_ is Even" }
421- }
422- ```
423-
424- ``` Output
425- 1 is Odd
426- 4 is Even
427- 3 is Odd
428- ```
429-
430- ## File parameter examples
395+ ### Read the content of a file with ` switch `
431396
432397Using the ` switch ` statement with the ** File** parameter is an efficient way to
433398process large files line by line. PowerShell streams the lines of the file to
@@ -450,10 +415,10 @@ switch -Regex -File .\README.md {
450415}
451416```
452417
453- The ` <filename> ` argument is interpreted as a wildcard expression , but it must
454- match only one file. The following example is the same as the previous one
455- except it uses a wildcard in the ` <filename> ` argument. This example works
456- because the wildcard pattern matches only one file.
418+ The ` <filename> ` argument accepts wildcard expressions , but it must match only
419+ one file. The following example is the same as the previous one except it uses
420+ a wildcard in the ` <filename> ` argument. This example works because the
421+ wildcard pattern matches only one file.
457422
458423``` powershell
459424switch -Regex -File .\README.* {
@@ -478,16 +443,15 @@ switch -File $fileEscaped { foo { 'Foo' } }
478443## See also
479444
480445- [ about_Break] [ 04 ]
481- - [ about_Continue] [ 06 ]
482- - [ about_If] [ 07 ]
483- - [ about_Script_Blocks] [ 08 ]
446+ - [ about_Continue] [ 05 ]
447+ - [ about_If] [ 06 ]
448+ - [ about_Script_Blocks] [ 07 ]
484449
485- <!-- updated link references -->
486- [ 01 ] : #file-parameter-examples
450+ <!-- link references -->
451+ [ 01 ] : #read-the-content-of-a- file-with-switch
487452[ 02 ] : #impact-of-string-conversion
488453[ 03 ] : about_Automatic_Variables.md
489454[ 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
455+ [ 05 ] : about_Continue.md
456+ [ 06 ] : about_If.md
457+ [ 07 ] : about_Script_Blocks.md
0 commit comments