質問

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