Reputation: 11
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
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