History cmdlets

There are 4 cmdlets for working with the history in a PowerShell session:

Add-History – adds an entry to the history
Clear-History – clears the history
Get-History -enables you see the current history
Invoke-History – enables you to run a line in your history

Get-History is the one I use the most.  You can use the UP arrow key to cycle back to an old command or use Invoke-History.

I’d only use Clear-History if I had a long running session and I was changing what I was working on.

Add-History is the odd one. I’ve never used it but it could be useful if you have a command you run often and you want to push it into the history when you start a session.

Be aware of  a  variable that controls history:

$MaximumHistoryCount  defaults to  4096 and is the maximum number of commands saved in the command history.

You can increase this up to 32768 if you desire but unless you put the command in your profile your next session will drop back to the default.

This entry was posted in Powershell. Bookmark the permalink.

One Response to History cmdlets

  1. Quirel says:

    Yes I think this is one of the gems of powershell, too few people use and know about it.

    I use Get-History more and more. It is convenient to document my work I have done.I even don’t use cmd anymore and start with powershell right away. So all my command line commands are logged and documented.

    As I often use it via Teamviewer and VNC sessions I save the history via:
    Get-History | ft -Wrap | Clip
    to get it right to my clipboard and paste it into some OneNote documents. ft -Wrap is important otherwise longer lines get truncated with ellipsis. So later I can see what exactly I have done…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s