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.
- ## Format-Table with wrapping and string trimming.
- function Format-TablePlus() {
- [CmdletBinding()]
- param(
- [Switch]
- ${AutoSize},
- [Switch]
- ${HideTableHeaders},
- [Switch]
- ${Wrap},
- [Parameter(Position=0)]
- [System.Object[]]
- ${Property},
- [System.Object]
- ${GroupBy},
- [System.String]
- ${View},
- [Switch]
- ${ShowError},
- [Switch]
- ${DisplayError},
- [Switch]
- ${Force},
- [ValidateSet('CoreOnly','EnumOnly','Both')]
- [System.String]
- ${Expand},
- [System.Int32]
- ${Width} = $Host.Ui.RawUI.BufferSize.Width,
- [Switch]
- ${PadEnd},
- [Parameter(ValueFromPipeline=$true)]
- [System.Management.Automation.PSObject]
- ${InputObject}
- )
- begin
- {
- try {
- $outBuffer = $null
- if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
- {
- $PSBoundParameters['OutBuffer'] = 1
- }
- # need to get rid of these before we pass this to the Format-Table cmdlet
- $null = $PSBoundParameters.Remove("Width")
- $null = $PSBoundParameters.Remove("TrimEnd")
- $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Format-Table', [System.Management.Automation.CommandTypes]::Cmdlet)
- ## I made the trimming optional, but defaulted it to on ;)
- $scriptCmd = &{
- if($PadEnd) {
- {& $wrappedCmd @PSBoundParameters | Out-String -Stream -Width $Width }
- } else {
- {& $wrappedCmd @PSBoundParameters | Out-String -Stream -Width $Width | % { $_.TrimEnd() } }
- }
- }
- $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
- $steppablePipeline.Begin($PSCmdlet)
- } catch {
- throw
- }
- }
- process
- {
- try {
- $steppablePipeline.Process($_)
- } catch {
- throw
- }
- }
- end
- {
- try {
- $steppablePipeline.End()
- } catch {
- throw
- }
- }
- <#
- .ForwardHelpTargetName Format-Table
- .ForwardHelpCategory Cmdlet
- #>
- }
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.
PowerShell Code Repository