user17508887
user17508887

Reputation: 57

controlling the script output using flags in PowerShell

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 ?

Upvotes: 0

Views: 505

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

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

Upvotes: 4

Related Questions