Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use Invoke-History in PowerShell on Windows

The Invoke-History cmdlet is a powerful tool in PowerShell that allows users to re-execute commands from their session history. This can be incredibly useful for system administrators and developers who need to quickly rerun previous commands without retyping them. Unlike traditional command-line interfaces, PowerShell provides a rich history feature that can be leveraged for efficiency and productivity.

In this article, we will explore how to use Invoke-History in PowerShell, its importance, and provide practical examples to illustrate its use. This feature is specific to PowerShell and does not have a direct equivalent in CMD. However, CMD users can use the doskey command to achieve similar functionality.

Examples:

  1. Viewing Command History: Before invoking a command from history, you might want to see a list of previously executed commands. Use the Get-History cmdlet for this purpose.

    Get-History

    This will display a numbered list of commands executed in the current session.

  2. Invoking a Command from History: To re-execute a command from the history, use the Invoke-History cmdlet followed by the ID of the command.

    Invoke-History -Id 3

    This will re-run the command with ID 3 from the history list.

  3. Using Aliases: PowerShell provides aliases for many cmdlets, making it quicker to type. For Invoke-History, the alias is r.

    r 3

    This achieves the same result as the previous example but with less typing.

  4. Re-running the Last Command: If you want to quickly re-run the last command executed, you can use:

    Invoke-History

    Or simply:

    r
  5. Combining with Other Cmdlets: You can also pipe the output of Get-History to Invoke-History for more complex scenarios.

    Get-History | Where-Object { $_.CommandLine -like "*Get-Process*" } | Invoke-History

    This will re-execute all commands from history that contain Get-Process.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.