#PSTip Send the last command executed to clipboard

When I start writing a script, I generally start at the shell and make sure the logic I am working on holds good. Also, when writing blog posts, I tend to use the console – either powershell.exe or powershell ISE console – and then copy the commands into a blog post.

So, generally, I end up copying the last command I executed to either a blog post or a script. So, here is a small snippet I use to achieve that!

(Get-History)[-1].commandline | clip

Simple! The trick to get the last executed command is to use an array index -1 which means the last item in the array. Now, all I need to do is put this in a simple function and put it in my profile for easy access:

Like this:

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.