Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Woznet/8f8a1ca390c30f89dbddf3b9b6b13baf to your computer and use it in GitHub Desktop.
Save Woznet/8f8a1ca390c30f89dbddf3b9b6b13baf to your computer and use it in GitHub Desktop.

Revisions

  1. Woznet created this gist Jul 3, 2025.
    28 changes: 28 additions & 0 deletions Add Duration property to HistoryInfo type.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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 $_
    }
    }
    60 changes: 60 additions & 0 deletions HistoryInfo.Format.ps1xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?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>