PowerShell Genie

Get-History

Use Get-History to return the commands run in the current session, with an id for each.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — PowerShell module reference

Syntax

1PS> Get-History [[-Id] <int64[]>] [[-Count] <int32>] [<CommonParameters>]
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>]
  1. Stage 1Get-History-Id -Count

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Get-History -Count 5

    1PS> Get-History -Count 5 | Select-Object Id, CommandLine
    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

    1PS> Get-History | Select-Object Id, @{ Name = 'Seconds'; Expression = {($_.EndExecutionTime - $_.StartExecutionTime).TotalSeconds} }, CommandLine
    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

    PowerShell module reference on Microsoft Learn

    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.

    Get-History reference on Microsoft Learn

    Recap

    Get-History Use Get-History to return the commands run in the current session, with an id for each.

    Next: Get-Location
    Genie guideGet-History

    Use Get-History to return the commands run in the current session, with an id for each. The console below is pre-loaded — run it, then change a value and run it again.