Home > Blockchain >  controlling the script output using flags in PowerShell
controlling the script output using flags in PowerShell

Time:11-08

I have a PowerShell script . I want the script output to have two options depending on flag set within script execution command :

for example I have the following script test.ps1:

Write-Host 'statement 1'`n 
Write-Host 'statement 2'`n 
Write-Host 'statement 3'`n 

I want when I run the script to have two options:

one option to print only statement 1.

the second option to print all statements (1, 2,and 3).

is that possible in PowerShell ?

CodePudding user response:

Add a -PrintAll switch parameter to your script/function, then use that to determine whether to call Write-Host or not:

param(
  [switch]$PrintAll
)

Write-Host 'Statement 1'

if($PrintAll){
  Write-Host 'Statement 2'
  Write-Host 'Statement 3'
}

Then invoke with:

.\script.ps1 -PrintAll
# or
Function-Name -PrintAll
  • Related