File tree Expand file tree Collapse file tree 5 files changed +105
-5
lines changed
5.1/Microsoft.PowerShell.Core/About
7.2/Microsoft.PowerShell.Core/About
7.3/Microsoft.PowerShell.Core/About
7.4/Microsoft.PowerShell.Core/About
7.5/Microsoft.PowerShell.Core/About Expand file tree Collapse file tree 5 files changed +105
-5
lines changed Original file line number Diff line number Diff line change 11---
22description : Explains how to use a switch to handle multiple `if` statements.
33Locale : en-US
4- ms.date : 01/27/2022
4+ ms.date : 02/28/2024
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
@@ -223,6 +223,26 @@ switch (4, 2)
223223It is four.
224224```
225225
226+ In this example, the ` switch ` statement is testing for the type of the value in
227+ the hashtable. You must use and expression that returns a boolean value to
228+ select the scriptblock to execute.
229+
230+ ``` powershell
231+ $var = @{A = 10; B = 'abc'}
232+
233+ foreach ($key in $var.Keys) {
234+ switch ($var[$key].GetType()) {
235+ { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
236+ { $_ -eq [string] } { "$key = $($var[$key])" }
237+ }
238+ }
239+ ```
240+
241+ ``` Output
242+ A + 10 = 20
243+ B = abc
244+ ```
245+
226246In this example, an object that's not a string or numerical data is passed to
227247the ` switch ` . The ` switch ` performs a string coercion on the object and
228248evaluates the outcome.
Original file line number Diff line number Diff line change 11---
22description : Explains how to use a switch to handle multiple `if` statements.
33Locale : en-US
4- ms.date : 01/27/2022
4+ ms.date : 02/28/2024
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.2&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223It is four.
224224```
225225
226+ In this example, the ` switch ` statement is testing for the type of the value in
227+ the hashtable. You must use and expression that returns a boolean value to
228+ select the scriptblock to execute.
229+
230+ ``` powershell
231+ $var = @{A = 10; B = 'abc'}
232+
233+ foreach ($key in $var.Keys) {
234+ switch ($var[$key].GetType()) {
235+ { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
236+ { $_ -eq [string] } { "$key = $($var[$key])" }
237+ }
238+ }
239+ ```
240+
241+ ``` Output
242+ A + 10 = 20
243+ B = abc
244+ ```
245+
226246In this example, an object that's not a string or numerical data is passed to
227247the ` switch ` . The ` switch ` performs a string coercion on the object and
228248evaluates the outcome.
Original file line number Diff line number Diff line change 11---
22description : Explains how to use a switch to handle multiple `if` statements.
33Locale : en-US
4- ms.date : 01/27/2022
4+ ms.date : 02/28/2024
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.3&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223It is four.
224224```
225225
226+ In this example, the ` switch ` statement is testing for the type of the value in
227+ the hashtable. You must use and expression that returns a boolean value to
228+ select the scriptblock to execute.
229+
230+ ``` powershell
231+ $var = @{A = 10; B = 'abc'}
232+
233+ foreach ($key in $var.Keys) {
234+ switch ($var[$key].GetType()) {
235+ { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
236+ { $_ -eq [string] } { "$key = $($var[$key])" }
237+ }
238+ }
239+ ```
240+
241+ ``` Output
242+ A + 10 = 20
243+ B = abc
244+ ```
245+
226246In this example, an object that's not a string or numerical data is passed to
227247the ` switch ` . The ` switch ` performs a string coercion on the object and
228248evaluates the outcome.
Original file line number Diff line number Diff line change 11---
22description : Explains how to use a switch to handle multiple `if` statements.
33Locale : en-US
4- ms.date : 01/27/2022
4+ ms.date : 02/28/2024
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.4&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223It is four.
224224```
225225
226+ In this example, the ` switch ` statement is testing for the type of the value in
227+ the hashtable. You must use and expression that returns a boolean value to
228+ select the scriptblock to execute.
229+
230+ ``` powershell
231+ $var = @{A = 10; B = 'abc'}
232+
233+ foreach ($key in $var.Keys) {
234+ switch ($var[$key].GetType()) {
235+ { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
236+ { $_ -eq [string] } { "$key = $($var[$key])" }
237+ }
238+ }
239+ ```
240+
241+ ``` Output
242+ A + 10 = 20
243+ B = abc
244+ ```
245+
226246In this example, an object that's not a string or numerical data is passed to
227247the ` switch ` . The ` switch ` performs a string coercion on the object and
228248evaluates the outcome.
Original file line number Diff line number Diff line change 11---
22description : Explains how to use a switch to handle multiple `if` statements.
33Locale : en-US
4- ms.date : 01/27/2022
4+ ms.date : 02/28/2024
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.5&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223It is four.
224224```
225225
226+ In this example, the ` switch ` statement is testing for the type of the value in
227+ the hashtable. You must use and expression that returns a boolean value to
228+ select the scriptblock to execute.
229+
230+ ``` powershell
231+ $var = @{A = 10; B = 'abc'}
232+
233+ foreach ($key in $var.Keys) {
234+ switch ($var[$key].GetType()) {
235+ { $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
236+ { $_ -eq [string] } { "$key = $($var[$key])" }
237+ }
238+ }
239+ ```
240+
241+ ``` Output
242+ A + 10 = 20
243+ B = abc
244+ ```
245+
226246In this example, an object that's not a string or numerical data is passed to
227247the ` switch ` . The ` switch ` performs a string coercion on the object and
228248evaluates the outcome.
You can’t perform that action at this time.
0 commit comments