dfernan
dfernan

Reputation: 387

PowerShell Pipeline or Input Argument

I could not find an answer to this question anywhere.

How can a PowerShell script written in a file (as I understand, it cannot be a function in order to allow the pipelining directly to the script) be made to accept either pipeline input or a file input argument? The following criteria must be met:

The idea is to achieve a similar behavior to standard Linux commands as in cat file | <command> [-o out] or <command> -f file [-o out], in PowerShell being cat file | .\script.ps1 [-OutFile out] or .\script.ps1 -File file [-OutFile out].

Upvotes: 2

Views: 597

Answers (1)

Joey
Joey

Reputation: 354356

You could have a script like this:

param([string]$Path)

if (!Path) {
  $scriptInput = $input
} else {
  $scriptInput = Get-Content $Path
}

# Do something with $scriptInput

Checking for either pipeline input or an argument is left as an exercise for you.

Upvotes: 2

Related Questions