https://www.2daygeek.com/display-date-time-linux-bash-history-command/
Found this article which seems to show equivalent how to for linux, using HISTTIMEFORMAT environment variable,
# history
1 2017-08-16 15:30:15 yum install -y mysql-server mysql-client
2 2017-08-16 15:30:15 service mysqld start
3 2017-08-16 15:30:15 sysdig proc.name=sshd
There may be a log, or a subscription, in windows event viewer that can be enabled or registered This feature may already exist in powershell...
Checked doskey /? but nothing jumps out.
❓ Is it possible to get details of when commands were executed, and what methods are available to achieve?
PowerShell's
Get-Historycmdlet (whose built-in aliases areghy,h, andhistory) outputsMicrosoft.PowerShell.Commands.HistoryInfoobjects, whose properties you can easily select for display withFormat-Table, for instance:Sample output:
Get-History's default for-display formatting shows theDurationproperty values instead ofStartExecutionTimein PowerShell (Core) 7+, and neither in Windows PowerShell.Of course, you're free to include both
DurationandStartExecutionTime.Note:
As of PowerShell 7.3.4,
Get-Historyonly ever shows the current session's history, not also the persisted history maintained by thePSReadLinemodule - see GitHub issue #12061 for a discussion.Since the solutions presented here build on
Get-History, they too are effective for the current-session history only, unlikebash'shistorybuiltin.If you want to change the default formatting, you have two fundamental options:
Author a
*.format.ps1xmlfile with the desired columns and load it withUpdate-FormatData-PrependPath, from your$PROFILEfile.This is the best approach, but non-trivial, unfortunately. See below.
Suboptimally, but more simply, create a custom function that wraps
Get-History; you may also redefine the aliases to point to your custom function (e.g.,Set-Alias h Format-HistoryCustom) - again, place these definitions in your$PROFILEfile:Adding a
StartExecutionTimecolumn toGet-History's output, via custom formatting data loaded from$PROFILE:The following implements the first approach discussed above. Note that on Windows PowerShell a
Durationcolumn is added as well (which is present by default in PowerShell (Core) 7+).The customized XML data used below was obtained by obtaining the current definitions as follows, followed by updating it by hand:
Run this once, which will place a
HistoryInfo.format.ps1xmlfile alongside your$PROFILEfile and append a command to load that file withUpdate-FormatData -PrependPathto$PROFILEitself.