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.
Add Duration property to Get-History output in Windows PowerShell 5.1
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 $_
}
}
<?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