Share via


PowerShell: How to use Switch Parameter?


What is Switch Parameter?

Help about_Functions_Advanced_Parameters

How to Use Switch Parameter?

Param(

    [Parameter(Mandatory=$false)]

    [Switch]$ColorText

    )

 

How to consume switch parameter?

Demo function if switch -colortext is included you get color text output if not plain text with default host color.

Function`` ``Demo``

 

{

    Param(

    [Parameter(Mandatory=$false)]

    [Switch]$ColorText

    )

 

if($ColorText -eq $false)

    {

        Write-Host "Plain Text"

    }

elseif($ColorText -eq $true)

    {

        Write-Host "Color Text" -ForegroundColor Green

    }

 

}