Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Parameter in Powershell

I've been looking all over, and i cant seem to find the way to make this work the way i want it to.

I'm new to PS Parameters and have something i think should be fairly simple...I want to be able to run a script and detect whether or not the switch is present. Such as:

myscript.ps1 -Deploy

-or-

myscript.ps1

Code, that i've been playing with:

function start-script {
    param (
        [parameter()]
        [switch]$Deploy
    )
    if ($Deploy.IsPresent) {
        Write-Host "True"
    }
    else {
    Write-Host "False"
    }
}
    
start-script

Running, this doesnt return any errors, but does not output the results of the write-host either. It basically does nothing. What am i doing wrong, and how would i get the correct outputs?

Thanks. M

EDIT: Adding Screenshot of what i am experiancing, since a few have said it should work or work with removing the .isPresent

enter image description here

like image 648
Mike Moss Avatar asked Dec 30 '25 23:12

Mike Moss


1 Answers

Makes sense, this is because in your function call (the last line of your script) you're never giving the function any argument. If you want to pass an argument to your .ps1 script from an outside source you should add a param(...) block at the top of your script. I'm following with the code you already have but, in this case, I don't think a function is needed at all.

Code could be simplified to this:

param (
    [switch] $Deploy
)

if ($Deploy) {
    Write-Host "True"
    return
}

Write-Host "False"

But if you want to use a function, you can leverage $PSBoundParameters as long as your function param block matches your script's one:

param (
    [switch] $Deploy
)

function Start-Script {
    param (
        [switch] $Deploy
    )
     
    if ($Deploy) {
        Write-Host "True"
        return
    }

    Write-Host "False"
}

Start-Script @PSBoundParameters
  • From PowerShell:
PS /> .\script.ps1
False

PS /> .\script.ps1 -Deploy
True
  • From CMD:
D:\> powershell -File script.ps1
False

D:\> powershell -File script.ps1 -Deploy
True

Edit

To add a bit more context to the answer:

  • [switch] $Deploy only exist in the scope of your function this is why we add the param(...) block at the top of the script too, so that the function can be invoked with the same arguments as you have invoked the .ps1 script.

  • Splatting with $PSBoundParameters can work with multiple functions.

Example:

param (
    [switch] $Deploy,
    [string] $Name
)

function Start-Script {
    param (
        [switch] $Deploy
    )

    "Deploy switch is: {0}" -f $Deploy.IsPresent
}

function SayHello {
    param(
        [string] $Name
    )

    "Hello $Name!"
}

# This is what mclayton was referring to in their comment.
# If the Argument $Name is called:
if ($PSBoundParameters.ContainsKey('Name')) {
    # Call the function
    SayHello @PSBoundParameters
}

Start-Script @PSBoundParameters
  • From PowerShell:
PS /> .\script.ps1 -Name world -Deploy
Hello world!
Deploy switch is: True
like image 107
Santiago Squarzon Avatar answered Jan 04 '26 04:01

Santiago Squarzon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!