Questions tagged [switch-parameter]

Questions relating to the PowerShell type [switch].

https://msdn.microsoft.com/en-us/library/system.management.automation.switchparameter_members(v=vs.85).aspx

Example Usage:

function Demo {
    [CmdletBinding()]
    param (
        [switch]$Switch
    )
    process {
        if($Switch.IsPresent) {
            "Switch Is Defined"
        } else {
            "Switch Not Used"
        }
    }
}
Demo 
Demo -Switch

Example Output

Switch Not Used
Switch Is Defined
7 questions
12
votes
2 answers

How to pass a switch parameter as a variable / via splatting in PowerShell?

If you have multiple parameters which require a value when calling a command or a script, I know you can pass it like this: $parameters = @{ name = "John" last_name = "Doe" } But if the command or script actually just expect -T to indicate…
Mason
  • 1,007
  • 1
  • 13
  • 31
3
votes
1 answer

What's the best way to make a recursive call with a SWITCH parameter?

In the below example I've got an IF statement to determine which parameters send to Demo when called recursively. If $y were a boolean value rather than a switch I could simply call Demo -x $x -y $y; but as a switch that's not a viable…
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
1
vote
1 answer

Powershell behaviour of [Switch]-Parameter

I'm trying to understand the behaviour of -switch parameters, especially when passing them to other functions. My underlying question is: How do I have to pass the variable of a switch parameter to another function with a switch parameter so the…
Stefan Lippeck
  • 381
  • 2
  • 17
1
vote
2 answers

PowerShell switch parameter doesn't work as expected

I have the following PowerShell function to help me do benchmarks. The idea is that you provide the command and a number and the function will run the code that number of times. After that the function will report the testing result, such as Min,…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
0
votes
2 answers

How to pass switch parameter from one powershell script to another?

I have two PowerShell scripts and use them on Linux with PowerShell Core, and want to pass the switch parameter from one.ps1 to second.ps1, but the $args is populated as a string with True and False sub-strings in it, which is not accepted by the…
absfdlf
  • 1
  • 1
0
votes
1 answer

Powershell C# commandlets Conditional switch parameters

I need to apply conditional switch parameter only if other parameter is provided. Can any body let me know how can I achieve this in C# I want following way command let accessible on powershell terminal. Edit Get-Parameter # this will process other…
Usman
  • 2,742
  • 4
  • 44
  • 82
0
votes
1 answer

Can I have a PSH function parameter that has $false if not specified, $true (or default value) when specified, or another value if given that value?

I'm not sure if this exists or is impossible, or would be best to just implement using two parameters, and I'm not sure if it would be more clear to ask using a generic context, or a more specific example, so I've provided both: More Generic I have…
Code Jockey
  • 6,611
  • 6
  • 33
  • 45