Skip to content

Instantly share code, notes, and snippets.

@trackd
Created November 3, 2023 15:55
Show Gist options
  • Save trackd/297487878b02d2192244c1c4f91c2690 to your computer and use it in GitHub Desktop.
Save trackd/297487878b02d2192244c1c4f91c2690 to your computer and use it in GitHub Desktop.
function table {
<#
format-table sugar that knows not to format if you're assigning to a variable.
it will automatically switch to Select-Object when assigned.
only implemented Property param atm, just testing it out for fun.
Set-Alias -Name ft -Value table -Force
to override default ft alias you need -Force.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[object] $InputObject,
[Parameter(Position = 0)]
[object[]] $Property
)
begin {
$assigned = ([System.Management.Automation.Language.Parser]::ParseInput($MyInvocation.Line, [ref]$null, [ref]$null)).EndBlock.Statements[0].operator
if ($assigned) {
$step = { Select-Object @PSBoundParameters }.GetSteppablePipeline()
}
else {
$step = { Format-Table @PSBoundParameters }.GetSteppablePipeline()
}
$step.Begin($PSCmdlet)
}
process {
$step.Process($InputObject)
}
end {
$step.End()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment