PowerShell Get-History

One command that I love to use in PowerShell is Get-History. It retrieves all of the commands that you have run in this session. You can use the id and count parameters to pick a single command or a number of commands including and prior to the id

e.g. Get-History -id 10 -count 5 gets command lines 6 through to 10 inclusive.

You can then also combine that with Set-Clipboard to copy commands to the clipboard so you can paste them into something else. Note that you can use clip.exe but that doesn’t support UTF-8 whereas Set-Clipboard does

(Get-History -id 10 -count 5).commandline | Set-Clipboard

Now those commands are ready to just Paste wherever you like 🙂

Twan van Beers

Twan is a senior consultant with over 20 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.

Leave a Reply

Your email address will not be published. Required fields are marked *