Skip to content

Commit 7d0a67e

Browse files
authored
Fixes #10911 - Add example using an expression (#10913)
* Add example using an expression * Improved the example
1 parent 6589c7c commit 7d0a67e

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

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

Lines changed: 21 additions & 1 deletion
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: 01/27/2022
4+
ms.date: 02/28/2024
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
@@ -223,6 +223,26 @@ switch (4, 2)
223223
It 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+
226246
In this example, an object that's not a string or numerical data is passed to
227247
the `switch`. The `switch` performs a string coercion on the object and
228248
evaluates the outcome.

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

Lines changed: 21 additions & 1 deletion
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: 01/27/2022
4+
ms.date: 02/28/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.2&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223
It 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+
226246
In this example, an object that's not a string or numerical data is passed to
227247
the `switch`. The `switch` performs a string coercion on the object and
228248
evaluates the outcome.

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

Lines changed: 21 additions & 1 deletion
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: 01/27/2022
4+
ms.date: 02/28/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.3&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223
It 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+
226246
In this example, an object that's not a string or numerical data is passed to
227247
the `switch`. The `switch` performs a string coercion on the object and
228248
evaluates the outcome.

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

Lines changed: 21 additions & 1 deletion
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: 01/27/2022
4+
ms.date: 02/28/2024
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
@@ -223,6 +223,26 @@ switch (4, 2)
223223
It 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+
226246
In this example, an object that's not a string or numerical data is passed to
227247
the `switch`. The `switch` performs a string coercion on the object and
228248
evaluates the outcome.

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

Lines changed: 21 additions & 1 deletion
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: 01/27/2022
4+
ms.date: 02/28/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.5&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Switch
@@ -223,6 +223,26 @@ switch (4, 2)
223223
It 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+
226246
In this example, an object that's not a string or numerical data is passed to
227247
the `switch`. The `switch` performs a string coercion on the object and
228248
evaluates the outcome.

0 commit comments

Comments
 (0)