Skip to content

Commit

Permalink
Fixes #10911 - Add example using an expression (#10913)
Browse files Browse the repository at this point in the history
* Add example using an expression

* Improved the example
  • Loading branch information
sdwheeler authored Feb 28, 2024
1 parent 6589c7c commit 7d0a67e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
22 changes: 21 additions & 1 deletion reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Explains how to use a switch to handle multiple `if` statements.
Locale: en-US
ms.date: 01/27/2022
ms.date: 02/28/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-5.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Switch
Expand Down Expand Up @@ -223,6 +223,26 @@ switch (4, 2)
It is four.
```

In this example, the `switch` statement is testing for the type of the value in
the hashtable. You must use and expression that returns a boolean value to
select the scriptblock to execute.

```powershell
$var = @{A = 10; B = 'abc'}
foreach ($key in $var.Keys) {
switch ($var[$key].GetType()) {
{ $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
{ $_ -eq [string] } { "$key = $($var[$key])" }
}
}
```

```Output
A + 10 = 20
B = abc
```

In this example, an object that's not a string or numerical data is passed to
the `switch`. The `switch` performs a string coercion on the object and
evaluates the outcome.
Expand Down
22 changes: 21 additions & 1 deletion reference/7.2/Microsoft.PowerShell.Core/About/about_Switch.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Explains how to use a switch to handle multiple `if` statements.
Locale: en-US
ms.date: 01/27/2022
ms.date: 02/28/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.2&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Switch
Expand Down Expand Up @@ -223,6 +223,26 @@ switch (4, 2)
It is four.
```

In this example, the `switch` statement is testing for the type of the value in
the hashtable. You must use and expression that returns a boolean value to
select the scriptblock to execute.

```powershell
$var = @{A = 10; B = 'abc'}
foreach ($key in $var.Keys) {
switch ($var[$key].GetType()) {
{ $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
{ $_ -eq [string] } { "$key = $($var[$key])" }
}
}
```

```Output
A + 10 = 20
B = abc
```

In this example, an object that's not a string or numerical data is passed to
the `switch`. The `switch` performs a string coercion on the object and
evaluates the outcome.
Expand Down
22 changes: 21 additions & 1 deletion reference/7.3/Microsoft.PowerShell.Core/About/about_Switch.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Explains how to use a switch to handle multiple `if` statements.
Locale: en-US
ms.date: 01/27/2022
ms.date: 02/28/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.3&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Switch
Expand Down Expand Up @@ -223,6 +223,26 @@ switch (4, 2)
It is four.
```

In this example, the `switch` statement is testing for the type of the value in
the hashtable. You must use and expression that returns a boolean value to
select the scriptblock to execute.

```powershell
$var = @{A = 10; B = 'abc'}
foreach ($key in $var.Keys) {
switch ($var[$key].GetType()) {
{ $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
{ $_ -eq [string] } { "$key = $($var[$key])" }
}
}
```

```Output
A + 10 = 20
B = abc
```

In this example, an object that's not a string or numerical data is passed to
the `switch`. The `switch` performs a string coercion on the object and
evaluates the outcome.
Expand Down
22 changes: 21 additions & 1 deletion reference/7.4/Microsoft.PowerShell.Core/About/about_Switch.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Explains how to use a switch to handle multiple `if` statements.
Locale: en-US
ms.date: 01/27/2022
ms.date: 02/28/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Switch
Expand Down Expand Up @@ -223,6 +223,26 @@ switch (4, 2)
It is four.
```

In this example, the `switch` statement is testing for the type of the value in
the hashtable. You must use and expression that returns a boolean value to
select the scriptblock to execute.

```powershell
$var = @{A = 10; B = 'abc'}
foreach ($key in $var.Keys) {
switch ($var[$key].GetType()) {
{ $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
{ $_ -eq [string] } { "$key = $($var[$key])" }
}
}
```

```Output
A + 10 = 20
B = abc
```

In this example, an object that's not a string or numerical data is passed to
the `switch`. The `switch` performs a string coercion on the object and
evaluates the outcome.
Expand Down
22 changes: 21 additions & 1 deletion reference/7.5/Microsoft.PowerShell.Core/About/about_Switch.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Explains how to use a switch to handle multiple `if` statements.
Locale: en-US
ms.date: 01/27/2022
ms.date: 02/28/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.5&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Switch
Expand Down Expand Up @@ -223,6 +223,26 @@ switch (4, 2)
It is four.
```

In this example, the `switch` statement is testing for the type of the value in
the hashtable. You must use and expression that returns a boolean value to
select the scriptblock to execute.

```powershell
$var = @{A = 10; B = 'abc'}
foreach ($key in $var.Keys) {
switch ($var[$key].GetType()) {
{ $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
{ $_ -eq [string] } { "$key = $($var[$key])" }
}
}
```

```Output
A + 10 = 20
B = abc
```

In this example, an object that's not a string or numerical data is passed to
the `switch`. The `switch` performs a string coercion on the object and
evaluates the outcome.
Expand Down

0 comments on commit 7d0a67e

Please sign in to comment.