From d9deea9f06e46d175d46567df51ba650e2cfea6f Mon Sep 17 00:00:00 2001 From: Bobby Reed Date: Thu, 30 May 2019 07:57:51 -0400 Subject: [PATCH 1/3] Fixes #4082 clarify scope defaults --- reference/6/PowerShellGet/Install-Module.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/reference/6/PowerShellGet/Install-Module.md b/reference/6/PowerShellGet/Install-Module.md index 9592cd011744..e251562f48e9 100644 --- a/reference/6/PowerShellGet/Install-Module.md +++ b/reference/6/PowerShellGet/Install-Module.md @@ -360,13 +360,11 @@ the computer: `$home\Documents\PowerShell\Modules` -When no **Scope** is defined, the default is set based on the current session: +When no **Scope** is defined, the default is set based on the PowerShellGet version. -- For an elevated PowerShell session, **Scope** defaults to **AllUsers**. -- For non-elevated PowerShell sessions in [PowerShellGet versions 2.0.0](https://www.powershellgallery.com/packages/PowerShellGet) - and above, the **Scope** is **CurrentUser**. -- For non-elevated PowerShell sessions in PowerShellGet versions 1.6.7 and earlier, **Scope** is - undefined, and `Install-Module` fails. +- In PowerShellGet versions 2.0.0 and above, the default is **CurrentUser**, which does not require + elevation for install. +- In PowerShellGet 1.x versions, the default is **AllUsers**, which requires elevation for install. ```yaml Type: String @@ -498,4 +496,4 @@ the **RequiredVersion** value. [Uninstall-Module](Uninstall-Module.md) -[Update-Module](Update-Module.md) \ No newline at end of file +[Update-Module](Update-Module.md) From 7c30d69120f13f43ffb0d48701af6c72327878bf Mon Sep 17 00:00:00 2001 From: Bobby Reed Date: Thu, 30 May 2019 08:50:37 -0400 Subject: [PATCH 2/3] Fixes #4082 Clarify switch script block conditions --- .../About/about_Switch.md | 100 ++++++++++++++---- .../About/about_Switch.md | 100 ++++++++++++++---- .../About/about_Switch.md | 100 ++++++++++++++---- .../About/about_Switch.md | 100 ++++++++++++++---- .../About/about_Switch.md | 100 ++++++++++++++---- 5 files changed, 410 insertions(+), 90 deletions(-) diff --git a/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md index 7e42ec071000..21140924c54f 100644 --- a/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,13 +166,11 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. If you | -| |you use **Wildcard**, **Regex** and **Exact** are ignored. | -| |Also, if the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. | +| |If the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. **Regex** and **Wildcard** are ignored. | -| |Also, if the match clause is not a string, this parameter | +| |match exactly. If the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -181,9 +179,15 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. **Wildcard** and **Exact** are ignored. Also, if the| +| |condition. If the| | |match clause is not a string, this parameter is ignored. | +> [!NOTE] +> When specifying conflicting values, like **Regex** and **Wildcard**, the last +> parameter specified takes precedence, and all conflicting parameters are ignored. +> Multiple instances of parameters are also permitted. However, only the +> last parameter used is effective. + In this example, there is no matching case so there is no output. ```powershell @@ -197,6 +201,27 @@ switch ("fourteen") } ``` +By adding the `Default` clause, you can perform an action when no other +conditions succeed. + +```powershell +switch ("fourteen") +{ + 1 {"It is one."; Break} + 2 {"It is two."; Break} + 3 {"It is three."; Break} + 4 {"It is four."; Break} + "fo*" {"That's too many."} + Default { + "No matches" + } +} +``` + +```Output +No matches +``` + For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -231,28 +256,67 @@ switch -Regex ($target) user@contoso.com is an email address ``` -Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. -However, only the last parameter used is effective. +A `Switch` statement condition may be either: + +- An expression whose value is compared to the input value +- A script block which should return `$true` if a condition is met. The + script block receives the current object to compare in the `$_` + automatic variable and is evaluated in its own scope. + +The action for each condition is independent of the actions in other +conditions. + +The following example demonstrates the use of script blocks as `Switch` +statement conditions. + +```powershell +switch ("Test") +{ + {$_ -is [String]} { + "Found a string" + } + "Test" { + "This executes as well" + } +} +``` + +```Output +Found a string +This executes as well +``` If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The Break keyword stops processing and exits the Switch statement. +The `Break` keyword stops processing and exits the `Switch` statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -If the condition is an expression or a script block, it is evaluated just -before it is compared to the value. The value is assigned to the `$_` -automatic variable and is available in the expression. The match succeeds -if the expression is true or matches the value. The expression is evaluated -in its own scope. +The following example processes an array of numbers and displays if they are +odd or even. Negative numbers are skipped with the `Continue` keyword. If a +non-number is encountered, execution is terminated with the `Break` keyword. -The `Default` keyword specifies a condition that is evaluated only when no -other conditions match the value. +```powershell +switch (1,4,-1,3,"Hello",2,1) +{ + {$_ -lt 0} { Continue } + {$_ -isnot [Int32]} { Break } + {$_ % 2} { + "$_ is Odd" + } + {-not ($_ % 2)} { + "$_ is Even" + } +} +``` -The action for each condition is independent of the actions in other -conditions. +```Output +1 is Odd +4 is Even +3 is Odd +``` ## SEE ALSO diff --git a/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md index 7e42ec071000..21140924c54f 100644 --- a/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,13 +166,11 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. If you | -| |you use **Wildcard**, **Regex** and **Exact** are ignored. | -| |Also, if the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. | +| |If the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. **Regex** and **Wildcard** are ignored. | -| |Also, if the match clause is not a string, this parameter | +| |match exactly. If the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -181,9 +179,15 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. **Wildcard** and **Exact** are ignored. Also, if the| +| |condition. If the| | |match clause is not a string, this parameter is ignored. | +> [!NOTE] +> When specifying conflicting values, like **Regex** and **Wildcard**, the last +> parameter specified takes precedence, and all conflicting parameters are ignored. +> Multiple instances of parameters are also permitted. However, only the +> last parameter used is effective. + In this example, there is no matching case so there is no output. ```powershell @@ -197,6 +201,27 @@ switch ("fourteen") } ``` +By adding the `Default` clause, you can perform an action when no other +conditions succeed. + +```powershell +switch ("fourteen") +{ + 1 {"It is one."; Break} + 2 {"It is two."; Break} + 3 {"It is three."; Break} + 4 {"It is four."; Break} + "fo*" {"That's too many."} + Default { + "No matches" + } +} +``` + +```Output +No matches +``` + For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -231,28 +256,67 @@ switch -Regex ($target) user@contoso.com is an email address ``` -Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. -However, only the last parameter used is effective. +A `Switch` statement condition may be either: + +- An expression whose value is compared to the input value +- A script block which should return `$true` if a condition is met. The + script block receives the current object to compare in the `$_` + automatic variable and is evaluated in its own scope. + +The action for each condition is independent of the actions in other +conditions. + +The following example demonstrates the use of script blocks as `Switch` +statement conditions. + +```powershell +switch ("Test") +{ + {$_ -is [String]} { + "Found a string" + } + "Test" { + "This executes as well" + } +} +``` + +```Output +Found a string +This executes as well +``` If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The Break keyword stops processing and exits the Switch statement. +The `Break` keyword stops processing and exits the `Switch` statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -If the condition is an expression or a script block, it is evaluated just -before it is compared to the value. The value is assigned to the `$_` -automatic variable and is available in the expression. The match succeeds -if the expression is true or matches the value. The expression is evaluated -in its own scope. +The following example processes an array of numbers and displays if they are +odd or even. Negative numbers are skipped with the `Continue` keyword. If a +non-number is encountered, execution is terminated with the `Break` keyword. -The `Default` keyword specifies a condition that is evaluated only when no -other conditions match the value. +```powershell +switch (1,4,-1,3,"Hello",2,1) +{ + {$_ -lt 0} { Continue } + {$_ -isnot [Int32]} { Break } + {$_ % 2} { + "$_ is Odd" + } + {-not ($_ % 2)} { + "$_ is Even" + } +} +``` -The action for each condition is independent of the actions in other -conditions. +```Output +1 is Odd +4 is Even +3 is Odd +``` ## SEE ALSO diff --git a/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md index 7e42ec071000..21140924c54f 100644 --- a/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,13 +166,11 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. If you | -| |you use **Wildcard**, **Regex** and **Exact** are ignored. | -| |Also, if the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. | +| |If the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. **Regex** and **Wildcard** are ignored. | -| |Also, if the match clause is not a string, this parameter | +| |match exactly. If the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -181,9 +179,15 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. **Wildcard** and **Exact** are ignored. Also, if the| +| |condition. If the| | |match clause is not a string, this parameter is ignored. | +> [!NOTE] +> When specifying conflicting values, like **Regex** and **Wildcard**, the last +> parameter specified takes precedence, and all conflicting parameters are ignored. +> Multiple instances of parameters are also permitted. However, only the +> last parameter used is effective. + In this example, there is no matching case so there is no output. ```powershell @@ -197,6 +201,27 @@ switch ("fourteen") } ``` +By adding the `Default` clause, you can perform an action when no other +conditions succeed. + +```powershell +switch ("fourteen") +{ + 1 {"It is one."; Break} + 2 {"It is two."; Break} + 3 {"It is three."; Break} + 4 {"It is four."; Break} + "fo*" {"That's too many."} + Default { + "No matches" + } +} +``` + +```Output +No matches +``` + For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -231,28 +256,67 @@ switch -Regex ($target) user@contoso.com is an email address ``` -Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. -However, only the last parameter used is effective. +A `Switch` statement condition may be either: + +- An expression whose value is compared to the input value +- A script block which should return `$true` if a condition is met. The + script block receives the current object to compare in the `$_` + automatic variable and is evaluated in its own scope. + +The action for each condition is independent of the actions in other +conditions. + +The following example demonstrates the use of script blocks as `Switch` +statement conditions. + +```powershell +switch ("Test") +{ + {$_ -is [String]} { + "Found a string" + } + "Test" { + "This executes as well" + } +} +``` + +```Output +Found a string +This executes as well +``` If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The Break keyword stops processing and exits the Switch statement. +The `Break` keyword stops processing and exits the `Switch` statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -If the condition is an expression or a script block, it is evaluated just -before it is compared to the value. The value is assigned to the `$_` -automatic variable and is available in the expression. The match succeeds -if the expression is true or matches the value. The expression is evaluated -in its own scope. +The following example processes an array of numbers and displays if they are +odd or even. Negative numbers are skipped with the `Continue` keyword. If a +non-number is encountered, execution is terminated with the `Break` keyword. -The `Default` keyword specifies a condition that is evaluated only when no -other conditions match the value. +```powershell +switch (1,4,-1,3,"Hello",2,1) +{ + {$_ -lt 0} { Continue } + {$_ -isnot [Int32]} { Break } + {$_ % 2} { + "$_ is Odd" + } + {-not ($_ % 2)} { + "$_ is Even" + } +} +``` -The action for each condition is independent of the actions in other -conditions. +```Output +1 is Odd +4 is Even +3 is Odd +``` ## SEE ALSO diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md index 7e42ec071000..21140924c54f 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,13 +166,11 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. If you | -| |you use **Wildcard**, **Regex** and **Exact** are ignored. | -| |Also, if the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. | +| |If the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. **Regex** and **Wildcard** are ignored. | -| |Also, if the match clause is not a string, this parameter | +| |match exactly. If the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -181,9 +179,15 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. **Wildcard** and **Exact** are ignored. Also, if the| +| |condition. If the| | |match clause is not a string, this parameter is ignored. | +> [!NOTE] +> When specifying conflicting values, like **Regex** and **Wildcard**, the last +> parameter specified takes precedence, and all conflicting parameters are ignored. +> Multiple instances of parameters are also permitted. However, only the +> last parameter used is effective. + In this example, there is no matching case so there is no output. ```powershell @@ -197,6 +201,27 @@ switch ("fourteen") } ``` +By adding the `Default` clause, you can perform an action when no other +conditions succeed. + +```powershell +switch ("fourteen") +{ + 1 {"It is one."; Break} + 2 {"It is two."; Break} + 3 {"It is three."; Break} + 4 {"It is four."; Break} + "fo*" {"That's too many."} + Default { + "No matches" + } +} +``` + +```Output +No matches +``` + For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -231,28 +256,67 @@ switch -Regex ($target) user@contoso.com is an email address ``` -Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. -However, only the last parameter used is effective. +A `Switch` statement condition may be either: + +- An expression whose value is compared to the input value +- A script block which should return `$true` if a condition is met. The + script block receives the current object to compare in the `$_` + automatic variable and is evaluated in its own scope. + +The action for each condition is independent of the actions in other +conditions. + +The following example demonstrates the use of script blocks as `Switch` +statement conditions. + +```powershell +switch ("Test") +{ + {$_ -is [String]} { + "Found a string" + } + "Test" { + "This executes as well" + } +} +``` + +```Output +Found a string +This executes as well +``` If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The Break keyword stops processing and exits the Switch statement. +The `Break` keyword stops processing and exits the `Switch` statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -If the condition is an expression or a script block, it is evaluated just -before it is compared to the value. The value is assigned to the `$_` -automatic variable and is available in the expression. The match succeeds -if the expression is true or matches the value. The expression is evaluated -in its own scope. +The following example processes an array of numbers and displays if they are +odd or even. Negative numbers are skipped with the `Continue` keyword. If a +non-number is encountered, execution is terminated with the `Break` keyword. -The `Default` keyword specifies a condition that is evaluated only when no -other conditions match the value. +```powershell +switch (1,4,-1,3,"Hello",2,1) +{ + {$_ -lt 0} { Continue } + {$_ -isnot [Int32]} { Break } + {$_ % 2} { + "$_ is Odd" + } + {-not ($_ % 2)} { + "$_ is Even" + } +} +``` -The action for each condition is independent of the actions in other -conditions. +```Output +1 is Odd +4 is Even +3 is Odd +``` ## SEE ALSO diff --git a/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md index 7e42ec071000..21140924c54f 100644 --- a/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,13 +166,11 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. If you | -| |you use **Wildcard**, **Regex** and **Exact** are ignored. | -| |Also, if the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. | +| |If the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. **Regex** and **Wildcard** are ignored. | -| |Also, if the match clause is not a string, this parameter | +| |match exactly. If the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -181,9 +179,15 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. **Wildcard** and **Exact** are ignored. Also, if the| +| |condition. If the| | |match clause is not a string, this parameter is ignored. | +> [!NOTE] +> When specifying conflicting values, like **Regex** and **Wildcard**, the last +> parameter specified takes precedence, and all conflicting parameters are ignored. +> Multiple instances of parameters are also permitted. However, only the +> last parameter used is effective. + In this example, there is no matching case so there is no output. ```powershell @@ -197,6 +201,27 @@ switch ("fourteen") } ``` +By adding the `Default` clause, you can perform an action when no other +conditions succeed. + +```powershell +switch ("fourteen") +{ + 1 {"It is one."; Break} + 2 {"It is two."; Break} + 3 {"It is three."; Break} + 4 {"It is four."; Break} + "fo*" {"That's too many."} + Default { + "No matches" + } +} +``` + +```Output +No matches +``` + For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -231,28 +256,67 @@ switch -Regex ($target) user@contoso.com is an email address ``` -Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. -However, only the last parameter used is effective. +A `Switch` statement condition may be either: + +- An expression whose value is compared to the input value +- A script block which should return `$true` if a condition is met. The + script block receives the current object to compare in the `$_` + automatic variable and is evaluated in its own scope. + +The action for each condition is independent of the actions in other +conditions. + +The following example demonstrates the use of script blocks as `Switch` +statement conditions. + +```powershell +switch ("Test") +{ + {$_ -is [String]} { + "Found a string" + } + "Test" { + "This executes as well" + } +} +``` + +```Output +Found a string +This executes as well +``` If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The Break keyword stops processing and exits the Switch statement. +The `Break` keyword stops processing and exits the `Switch` statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -If the condition is an expression or a script block, it is evaluated just -before it is compared to the value. The value is assigned to the `$_` -automatic variable and is available in the expression. The match succeeds -if the expression is true or matches the value. The expression is evaluated -in its own scope. +The following example processes an array of numbers and displays if they are +odd or even. Negative numbers are skipped with the `Continue` keyword. If a +non-number is encountered, execution is terminated with the `Break` keyword. -The `Default` keyword specifies a condition that is evaluated only when no -other conditions match the value. +```powershell +switch (1,4,-1,3,"Hello",2,1) +{ + {$_ -lt 0} { Continue } + {$_ -isnot [Int32]} { Break } + {$_ % 2} { + "$_ is Odd" + } + {-not ($_ % 2)} { + "$_ is Even" + } +} +``` -The action for each condition is independent of the actions in other -conditions. +```Output +1 is Odd +4 is Even +3 is Odd +``` ## SEE ALSO From a6ae759f81c12495afc9651991961eea345a787d Mon Sep 17 00:00:00 2001 From: Bobby Reed Date: Thu, 30 May 2019 08:54:19 -0400 Subject: [PATCH 3/3] reverting switch --- .../About/about_Switch.md | 100 ++++-------------- .../About/about_Switch.md | 100 ++++-------------- .../About/about_Switch.md | 100 ++++-------------- .../About/about_Switch.md | 100 ++++-------------- .../About/about_Switch.md | 100 ++++-------------- 5 files changed, 90 insertions(+), 410 deletions(-) diff --git a/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md index 21140924c54f..7e42ec071000 100644 --- a/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/3.0/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,11 +166,13 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. | -| |If the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. If you | +| |you use **Wildcard**, **Regex** and **Exact** are ignored. | +| |Also, if the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. If the match clause is not a string, this parameter | +| |match exactly. **Regex** and **Wildcard** are ignored. | +| |Also, if the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -179,15 +181,9 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. If the| +| |condition. **Wildcard** and **Exact** are ignored. Also, if the| | |match clause is not a string, this parameter is ignored. | -> [!NOTE] -> When specifying conflicting values, like **Regex** and **Wildcard**, the last -> parameter specified takes precedence, and all conflicting parameters are ignored. -> Multiple instances of parameters are also permitted. However, only the -> last parameter used is effective. - In this example, there is no matching case so there is no output. ```powershell @@ -201,27 +197,6 @@ switch ("fourteen") } ``` -By adding the `Default` clause, you can perform an action when no other -conditions succeed. - -```powershell -switch ("fourteen") -{ - 1 {"It is one."; Break} - 2 {"It is two."; Break} - 3 {"It is three."; Break} - 4 {"It is four."; Break} - "fo*" {"That's too many."} - Default { - "No matches" - } -} -``` - -```Output -No matches -``` - For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -256,67 +231,28 @@ switch -Regex ($target) user@contoso.com is an email address ``` -A `Switch` statement condition may be either: - -- An expression whose value is compared to the input value -- A script block which should return `$true` if a condition is met. The - script block receives the current object to compare in the `$_` - automatic variable and is evaluated in its own scope. - -The action for each condition is independent of the actions in other -conditions. - -The following example demonstrates the use of script blocks as `Switch` -statement conditions. - -```powershell -switch ("Test") -{ - {$_ -is [String]} { - "Found a string" - } - "Test" { - "This executes as well" - } -} -``` - -```Output -Found a string -This executes as well -``` +Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. +However, only the last parameter used is effective. If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The `Break` keyword stops processing and exits the `Switch` statement. +The Break keyword stops processing and exits the Switch statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -The following example processes an array of numbers and displays if they are -odd or even. Negative numbers are skipped with the `Continue` keyword. If a -non-number is encountered, execution is terminated with the `Break` keyword. +If the condition is an expression or a script block, it is evaluated just +before it is compared to the value. The value is assigned to the `$_` +automatic variable and is available in the expression. The match succeeds +if the expression is true or matches the value. The expression is evaluated +in its own scope. -```powershell -switch (1,4,-1,3,"Hello",2,1) -{ - {$_ -lt 0} { Continue } - {$_ -isnot [Int32]} { Break } - {$_ % 2} { - "$_ is Odd" - } - {-not ($_ % 2)} { - "$_ is Even" - } -} -``` +The `Default` keyword specifies a condition that is evaluated only when no +other conditions match the value. -```Output -1 is Odd -4 is Even -3 is Odd -``` +The action for each condition is independent of the actions in other +conditions. ## SEE ALSO diff --git a/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md index 21140924c54f..7e42ec071000 100644 --- a/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/4.0/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,11 +166,13 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. | -| |If the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. If you | +| |you use **Wildcard**, **Regex** and **Exact** are ignored. | +| |Also, if the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. If the match clause is not a string, this parameter | +| |match exactly. **Regex** and **Wildcard** are ignored. | +| |Also, if the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -179,15 +181,9 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. If the| +| |condition. **Wildcard** and **Exact** are ignored. Also, if the| | |match clause is not a string, this parameter is ignored. | -> [!NOTE] -> When specifying conflicting values, like **Regex** and **Wildcard**, the last -> parameter specified takes precedence, and all conflicting parameters are ignored. -> Multiple instances of parameters are also permitted. However, only the -> last parameter used is effective. - In this example, there is no matching case so there is no output. ```powershell @@ -201,27 +197,6 @@ switch ("fourteen") } ``` -By adding the `Default` clause, you can perform an action when no other -conditions succeed. - -```powershell -switch ("fourteen") -{ - 1 {"It is one."; Break} - 2 {"It is two."; Break} - 3 {"It is three."; Break} - 4 {"It is four."; Break} - "fo*" {"That's too many."} - Default { - "No matches" - } -} -``` - -```Output -No matches -``` - For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -256,67 +231,28 @@ switch -Regex ($target) user@contoso.com is an email address ``` -A `Switch` statement condition may be either: - -- An expression whose value is compared to the input value -- A script block which should return `$true` if a condition is met. The - script block receives the current object to compare in the `$_` - automatic variable and is evaluated in its own scope. - -The action for each condition is independent of the actions in other -conditions. - -The following example demonstrates the use of script blocks as `Switch` -statement conditions. - -```powershell -switch ("Test") -{ - {$_ -is [String]} { - "Found a string" - } - "Test" { - "This executes as well" - } -} -``` - -```Output -Found a string -This executes as well -``` +Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. +However, only the last parameter used is effective. If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The `Break` keyword stops processing and exits the `Switch` statement. +The Break keyword stops processing and exits the Switch statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -The following example processes an array of numbers and displays if they are -odd or even. Negative numbers are skipped with the `Continue` keyword. If a -non-number is encountered, execution is terminated with the `Break` keyword. +If the condition is an expression or a script block, it is evaluated just +before it is compared to the value. The value is assigned to the `$_` +automatic variable and is available in the expression. The match succeeds +if the expression is true or matches the value. The expression is evaluated +in its own scope. -```powershell -switch (1,4,-1,3,"Hello",2,1) -{ - {$_ -lt 0} { Continue } - {$_ -isnot [Int32]} { Break } - {$_ % 2} { - "$_ is Odd" - } - {-not ($_ % 2)} { - "$_ is Even" - } -} -``` +The `Default` keyword specifies a condition that is evaluated only when no +other conditions match the value. -```Output -1 is Odd -4 is Even -3 is Odd -``` +The action for each condition is independent of the actions in other +conditions. ## SEE ALSO diff --git a/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md index 21140924c54f..7e42ec071000 100644 --- a/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/5.0/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,11 +166,13 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. | -| |If the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. If you | +| |you use **Wildcard**, **Regex** and **Exact** are ignored. | +| |Also, if the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. If the match clause is not a string, this parameter | +| |match exactly. **Regex** and **Wildcard** are ignored. | +| |Also, if the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -179,15 +181,9 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. If the| +| |condition. **Wildcard** and **Exact** are ignored. Also, if the| | |match clause is not a string, this parameter is ignored. | -> [!NOTE] -> When specifying conflicting values, like **Regex** and **Wildcard**, the last -> parameter specified takes precedence, and all conflicting parameters are ignored. -> Multiple instances of parameters are also permitted. However, only the -> last parameter used is effective. - In this example, there is no matching case so there is no output. ```powershell @@ -201,27 +197,6 @@ switch ("fourteen") } ``` -By adding the `Default` clause, you can perform an action when no other -conditions succeed. - -```powershell -switch ("fourteen") -{ - 1 {"It is one."; Break} - 2 {"It is two."; Break} - 3 {"It is three."; Break} - 4 {"It is four."; Break} - "fo*" {"That's too many."} - Default { - "No matches" - } -} -``` - -```Output -No matches -``` - For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -256,67 +231,28 @@ switch -Regex ($target) user@contoso.com is an email address ``` -A `Switch` statement condition may be either: - -- An expression whose value is compared to the input value -- A script block which should return `$true` if a condition is met. The - script block receives the current object to compare in the `$_` - automatic variable and is evaluated in its own scope. - -The action for each condition is independent of the actions in other -conditions. - -The following example demonstrates the use of script blocks as `Switch` -statement conditions. - -```powershell -switch ("Test") -{ - {$_ -is [String]} { - "Found a string" - } - "Test" { - "This executes as well" - } -} -``` - -```Output -Found a string -This executes as well -``` +Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. +However, only the last parameter used is effective. If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The `Break` keyword stops processing and exits the `Switch` statement. +The Break keyword stops processing and exits the Switch statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -The following example processes an array of numbers and displays if they are -odd or even. Negative numbers are skipped with the `Continue` keyword. If a -non-number is encountered, execution is terminated with the `Break` keyword. +If the condition is an expression or a script block, it is evaluated just +before it is compared to the value. The value is assigned to the `$_` +automatic variable and is available in the expression. The match succeeds +if the expression is true or matches the value. The expression is evaluated +in its own scope. -```powershell -switch (1,4,-1,3,"Hello",2,1) -{ - {$_ -lt 0} { Continue } - {$_ -isnot [Int32]} { Break } - {$_ % 2} { - "$_ is Odd" - } - {-not ($_ % 2)} { - "$_ is Even" - } -} -``` +The `Default` keyword specifies a condition that is evaluated only when no +other conditions match the value. -```Output -1 is Odd -4 is Even -3 is Odd -``` +The action for each condition is independent of the actions in other +conditions. ## SEE ALSO diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md index 21140924c54f..7e42ec071000 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,11 +166,13 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. | -| |If the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. If you | +| |you use **Wildcard**, **Regex** and **Exact** are ignored. | +| |Also, if the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. If the match clause is not a string, this parameter | +| |match exactly. **Regex** and **Wildcard** are ignored. | +| |Also, if the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -179,15 +181,9 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. If the| +| |condition. **Wildcard** and **Exact** are ignored. Also, if the| | |match clause is not a string, this parameter is ignored. | -> [!NOTE] -> When specifying conflicting values, like **Regex** and **Wildcard**, the last -> parameter specified takes precedence, and all conflicting parameters are ignored. -> Multiple instances of parameters are also permitted. However, only the -> last parameter used is effective. - In this example, there is no matching case so there is no output. ```powershell @@ -201,27 +197,6 @@ switch ("fourteen") } ``` -By adding the `Default` clause, you can perform an action when no other -conditions succeed. - -```powershell -switch ("fourteen") -{ - 1 {"It is one."; Break} - 2 {"It is two."; Break} - 3 {"It is three."; Break} - 4 {"It is four."; Break} - "fo*" {"That's too many."} - Default { - "No matches" - } -} -``` - -```Output -No matches -``` - For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -256,67 +231,28 @@ switch -Regex ($target) user@contoso.com is an email address ``` -A `Switch` statement condition may be either: - -- An expression whose value is compared to the input value -- A script block which should return `$true` if a condition is met. The - script block receives the current object to compare in the `$_` - automatic variable and is evaluated in its own scope. - -The action for each condition is independent of the actions in other -conditions. - -The following example demonstrates the use of script blocks as `Switch` -statement conditions. - -```powershell -switch ("Test") -{ - {$_ -is [String]} { - "Found a string" - } - "Test" { - "This executes as well" - } -} -``` - -```Output -Found a string -This executes as well -``` +Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. +However, only the last parameter used is effective. If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The `Break` keyword stops processing and exits the `Switch` statement. +The Break keyword stops processing and exits the Switch statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -The following example processes an array of numbers and displays if they are -odd or even. Negative numbers are skipped with the `Continue` keyword. If a -non-number is encountered, execution is terminated with the `Break` keyword. +If the condition is an expression or a script block, it is evaluated just +before it is compared to the value. The value is assigned to the `$_` +automatic variable and is available in the expression. The match succeeds +if the expression is true or matches the value. The expression is evaluated +in its own scope. -```powershell -switch (1,4,-1,3,"Hello",2,1) -{ - {$_ -lt 0} { Continue } - {$_ -isnot [Int32]} { Break } - {$_ % 2} { - "$_ is Odd" - } - {-not ($_ % 2)} { - "$_ is Even" - } -} -``` +The `Default` keyword specifies a condition that is evaluated only when no +other conditions match the value. -```Output -1 is Odd -4 is Even -3 is Odd -``` +The action for each condition is independent of the actions in other +conditions. ## SEE ALSO diff --git a/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md b/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md index 21140924c54f..7e42ec071000 100644 --- a/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md +++ b/reference/6/Microsoft.PowerShell.Core/About/about_Switch.md @@ -166,11 +166,13 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one |Parameter |Description | |------------|----------------------------------------------------------| -|**Wildcard**|Indicates that the condition is a wildcard string. | -| |If the match clause is not a string, the parameter is| +|**Wildcard**|Indicates that the condition is a wildcard string. If you | +| |you use **Wildcard**, **Regex** and **Exact** are ignored. | +| |Also, if the match clause is not a string, the parameter is| | |ignored. | |**Exact** |Indicates that the match clause, if it is a string, must | -| |match exactly. If the match clause is not a string, this parameter | +| |match exactly. **Regex** and **Wildcard** are ignored. | +| |Also, if the match clause is not a string, this parameter | | |is ignored. | |**CaseSensitive**|Performs a case-sensitive match. If the match clause is not| | |a string, this parameter is ignored. | @@ -179,15 +181,9 @@ conditions. It is equivalent to an `Else` clause in an `If` statement. Only one | |used. Each line of the file is read and evaluated by the | | |`Switch` statement. | |**Regex** |Performs regular expression matching of the value to the | -| |condition. If the| +| |condition. **Wildcard** and **Exact** are ignored. Also, if the| | |match clause is not a string, this parameter is ignored. | -> [!NOTE] -> When specifying conflicting values, like **Regex** and **Wildcard**, the last -> parameter specified takes precedence, and all conflicting parameters are ignored. -> Multiple instances of parameters are also permitted. However, only the -> last parameter used is effective. - In this example, there is no matching case so there is no output. ```powershell @@ -201,27 +197,6 @@ switch ("fourteen") } ``` -By adding the `Default` clause, you can perform an action when no other -conditions succeed. - -```powershell -switch ("fourteen") -{ - 1 {"It is one."; Break} - 2 {"It is two."; Break} - 3 {"It is three."; Break} - 4 {"It is four."; Break} - "fo*" {"That's too many."} - Default { - "No matches" - } -} -``` - -```Output -No matches -``` - For the word "fourteen" to match a case you must use the `-Wildcard` or `-Regex` parameter. @@ -256,67 +231,28 @@ switch -Regex ($target) user@contoso.com is an email address ``` -A `Switch` statement condition may be either: - -- An expression whose value is compared to the input value -- A script block which should return `$true` if a condition is met. The - script block receives the current object to compare in the `$_` - automatic variable and is evaluated in its own scope. - -The action for each condition is independent of the actions in other -conditions. - -The following example demonstrates the use of script blocks as `Switch` -statement conditions. - -```powershell -switch ("Test") -{ - {$_ -is [String]} { - "Found a string" - } - "Test" { - "This executes as well" - } -} -``` - -```Output -Found a string -This executes as well -``` +Multiple instances of **Regex**, **Wildcard**, or **Exact** are permitted. +However, only the last parameter used is effective. If the value matches multiple conditions, the action for each condition is executed. To change this behavior, use the `Break` or `Continue` keywords. -The `Break` keyword stops processing and exits the `Switch` statement. +The Break keyword stops processing and exits the Switch statement. The `Continue` keyword stops processing the current value, but continues processing any subsequent values. -The following example processes an array of numbers and displays if they are -odd or even. Negative numbers are skipped with the `Continue` keyword. If a -non-number is encountered, execution is terminated with the `Break` keyword. +If the condition is an expression or a script block, it is evaluated just +before it is compared to the value. The value is assigned to the `$_` +automatic variable and is available in the expression. The match succeeds +if the expression is true or matches the value. The expression is evaluated +in its own scope. -```powershell -switch (1,4,-1,3,"Hello",2,1) -{ - {$_ -lt 0} { Continue } - {$_ -isnot [Int32]} { Break } - {$_ % 2} { - "$_ is Odd" - } - {-not ($_ % 2)} { - "$_ is Even" - } -} -``` +The `Default` keyword specifies a condition that is evaluated only when no +other conditions match the value. -```Output -1 is Odd -4 is Even -3 is Odd -``` +The action for each condition is independent of the actions in other +conditions. ## SEE ALSO