Created
July 3, 2025 19:49
-
-
Save Woznet/8f8a1ca390c30f89dbddf3b9b6b13baf to your computer and use it in GitHub Desktop.
Add Duration property to Get-History output in Windows PowerShell 5.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if ($PSVersionTable.PSEdition -ne 'Core') { | |
| try { | |
| Update-TypeData -TypeName Microsoft.PowerShell.Commands.HistoryInfo -MemberType ScriptProperty -MemberName Duration -Value { | |
| [OutputType([timespan])] | |
| param() | |
| if ($this.EndExecutionTime -and $this.StartExecutionTime) { | |
| return ($this.EndExecutionTime - $this.StartExecutionTime) | |
| } | |
| else { | |
| return $null | |
| } | |
| } | |
| Update-FormatData -PrependPath (Join-Path $PSScriptRoot 'HistoryInfo.format.ps1xml' -Resolve) | |
| } | |
| catch { | |
| [ErrorRecord]$e = $_ | |
| [pscustomobject]@{ | |
| Type = $e.Exception.GetType().FullName | |
| Exception = $e.Exception.Message | |
| Reason = $e.CategoryInfo.Reason | |
| Target = $e.CategoryInfo.TargetName | |
| Script = $e.InvocationInfo.ScriptName | |
| Message = $e.InvocationInfo.PositionMessage | |
| } | |
| Write-Error -ErrorRecord $_ | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <Configuration> | |
| <ViewDefinitions> | |
| <View> | |
| <Name>History</Name> | |
| <ViewSelectedBy> | |
| <TypeName>Microsoft.PowerShell.Commands.HistoryInfo</TypeName> | |
| </ViewSelectedBy> | |
| <TableControl> | |
| <TableHeaders> | |
| <TableColumnHeader> | |
| <Width>4</Width> | |
| <Alignment>Right</Alignment> | |
| </TableColumnHeader> | |
| <TableColumnHeader> | |
| <Label>Duration</Label> | |
| <Width>12</Width> | |
| <Alignment>Right</Alignment> | |
| </TableColumnHeader> | |
| <TableColumnHeader /> | |
| </TableHeaders> | |
| <TableRowEntries> | |
| <TableRowEntry> | |
| <TableColumnItems> | |
| <TableColumnItem> | |
| <PropertyName>Id</PropertyName> | |
| </TableColumnItem> | |
| <TableColumnItem> | |
| <ScriptBlock> | |
| if ($_.Duration.TotalHours -ge 10) { return ('{0}:{1:mm}:{1:ss}.{1:fff}' -f [int]$_.Duration.TotalHours, $_.Duration) } | |
| elseif ($_.Duration.TotalHours -ge 1) { return $_.Duration.ToString('h\:mm\:ss\.fff') } | |
| elseif ($_.Duration.TotalMinutes -ge 1) { return $_.Duration.ToString('m\:ss\.fff') } | |
| else { return $_.Duration.ToString('s\.fff') } | |
| </ScriptBlock> | |
| </TableColumnItem> | |
| <TableColumnItem> | |
| <PropertyName>CommandLine</PropertyName> | |
| </TableColumnItem> | |
| </TableColumnItems> | |
| </TableRowEntry> | |
| </TableRowEntries> | |
| </TableControl> | |
| </View> | |
| <View> | |
| <Name>History</Name> | |
| <ViewSelectedBy> | |
| <TypeName>Microsoft.PowerShell.Commands.HistoryInfo</TypeName> | |
| </ViewSelectedBy> | |
| <WideControl> | |
| <WideEntries> | |
| <WideEntry> | |
| <WideItem> | |
| <PropertyName>CommandLine</PropertyName> | |
| </WideItem> | |
| </WideEntry> | |
| </WideEntries> | |
| </WideControl> | |
| </View> | |
| </ViewDefinitions> | |
| </Configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment