0

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 ?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.