Frage

I want to conditionally apply Format-Wide to a pipeline:

Get-ChildItem | Format-Wide

How can I make | Format-Wide part conditional on a variable? For example, apply | Format-Wide only if $condition is True.

Edited: the following is what I want to achieve:

function format-conditional {
  param ([bool]$condition)
  if ($condition) {$input | Format-Wide -Column 3 } 
  else {$input}
}

Invoke-Expression ("Get-ChildItem $Args") |
%{
    $fore = $Host.UI.RawUI.ForegroundColor
    $Host.UI.RawUI.ForegroundColor = 'Green'
    echo $_
    $Host.UI.RawUI.ForegroundColor = $fore
} | format-conditional $false

But with this the Green color is gone.

War es hilfreich?

Lösung

You'll need to create your own function for that if you want to do it in the pipeline:

function format-conditional
{
  param ([bool]$condition)
  if ($condition) {$input | format-wide } 
  else {$input}
}

 $test = $true

 gci | format-conditional $test
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top