Dan Pretselman
Dan Pretselman

Reputation: 11

It there a way to add a pipeline to a cmdlet conditionally/dynamically?

In PowerShell, How I can add a pipeline command dynamically/conditionally?

For example, I have something like this:

If (some condition) { Get-Content $FilePath | Out-String } else { Get-Content $FilePath }

the question is how to add the "| Out-String" without writing the whole IF statement as above? Maybe we can use some inline IIF statement and etc?

like : Get-Content $FilePath if(true) { |Out-String}

Thanks

Upvotes: 0

Views: 118

Answers (1)

postanote
postanote

Reputation: 16106

The closest you will get to what you are after is this.

if (some condition) { Get-Content $FilePath | Out-String } else { Get-Content $FilePath }

... is not this...

Get-Content $FilePath if(true) { }

... .but this...

If ($FileContent = Get-Content -Path 'D:\temp\abc.txt')
{$FileContent | Out-String}

Upvotes: 1

Related Questions