سؤال

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.

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top