Get-History
Use Get-History to return the commands run in the current session, with an id for each.
Documentation: Microsoft Learn — PowerShell module reference
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Get-History [[-Id] <int64[]>] [[-Count] <int32>] [<CommonParameters>]- Stage 1Get-History
-Id -Count
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Get-History -Count 5
Show reference output
Sample output · fictional data, shaped like the real thing
Id CommandLine -- ----------- 12 Get-Service Spooler 13 Get-Process | Sort-Object CPU -Descending 14 Get-ChildItem C:\Temp
Select Id
Show reference output
Sample output · fictional data, shaped like the real thing
Id Seconds CommandLine -- ------- ----------- 12 0.04 Get-Service Spooler 13 0.22 Get-Process | Sort-Object CPU -Descending 14 0.01 Get-ChildItem C:\Temp
Parameters
Parameter definitions:
-Id-Count
Practical use and troubleshooting
Practical scenario: Use Get-History when you need to return the commands run in the current session, with an id for each. Review syntax and sample output, then try an example in the browser lab. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: An empty history can be expected in a new session or noninteractive process. Do not confuse it with PSReadLine's saved history file or assume history is shared across consoles. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Get-Command. Get-History reads the current session's command history; Get-Command discovers available commands. History is evidence of entered commands, not a catalog of supported cmdlets. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
Get-History Use Get-History to return the commands run in the current session, with an id for each.
Next: Get-Location