PoshCode Logo PowerShell Code Repository

Format-TablePlus (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1187"></script>download | new post

This is a wrapper function for Format-Table that adds a -Width parameter, and a -PadEnd parameter (without which it trims the end of every line of output. Set -PadEnd to get the original Format-Table behavior of adding needless whitespace on the end of every line of output.

  1. ## Format-Table with wrapping and string trimming.
  2. function Format-TablePlus() {
  3. [CmdletBinding()]
  4. param(
  5.    [Switch]
  6.    ${AutoSize},
  7.  
  8.    [Switch]
  9.    ${HideTableHeaders},
  10.  
  11.    [Switch]
  12.    ${Wrap},
  13.  
  14.    [Parameter(Position=0)]
  15.    [System.Object[]]
  16.    ${Property},
  17.  
  18.    [System.Object]
  19.    ${GroupBy},
  20.  
  21.    [System.String]
  22.    ${View},
  23.  
  24.    [Switch]
  25.    ${ShowError},
  26.  
  27.    [Switch]
  28.    ${DisplayError},
  29.  
  30.    [Switch]
  31.    ${Force},
  32.  
  33.    [ValidateSet('CoreOnly','EnumOnly','Both')]
  34.    [System.String]
  35.    ${Expand},
  36.  
  37.    [System.Int32]
  38.    ${Width} = $Host.Ui.RawUI.BufferSize.Width,
  39.  
  40.    [Switch]
  41.    ${PadEnd},
  42.  
  43.    [Parameter(ValueFromPipeline=$true)]
  44.    [System.Management.Automation.PSObject]
  45.    ${InputObject}
  46. )
  47.  
  48. begin
  49. {
  50.    try {
  51.       $outBuffer = $null
  52.       if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  53.       {
  54.          $PSBoundParameters['OutBuffer'] = 1
  55.       }
  56.       # need to get rid of these before we pass this to the Format-Table cmdlet
  57.       $null = $PSBoundParameters.Remove("Width")
  58.       $null = $PSBoundParameters.Remove("TrimEnd")
  59.      
  60.       $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Format-Table', [System.Management.Automation.CommandTypes]::Cmdlet)
  61.      
  62.       ## I made the trimming optional, but defaulted it to on ;)
  63.       $scriptCmd = &{
  64.          if($PadEnd) {
  65.             {& $wrappedCmd @PSBoundParameters | Out-String -Stream -Width $Width }
  66.          } else {
  67.             {& $wrappedCmd @PSBoundParameters | Out-String -Stream -Width $Width | % { $_.TrimEnd() } }
  68.          }
  69.       }
  70.       $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
  71.       $steppablePipeline.Begin($PSCmdlet)
  72.    } catch {
  73.       throw
  74.    }
  75. }
  76.  
  77. process
  78. {
  79.    try {
  80.       $steppablePipeline.Process($_)
  81.    } catch {
  82.       throw
  83.    }
  84. }
  85.  
  86. end
  87. {
  88.    try {
  89.       $steppablePipeline.End()
  90.    } catch {
  91.       throw
  92.    }
  93. }
  94. <#
  95.  
  96. .ForwardHelpTargetName Format-Table
  97. .ForwardHelpCategory Cmdlet
  98.  
  99. #>
  100. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me