2

#PSTip Send the last command executed to clipboard

Note: This tip requires PowerShell 2.0 or above.

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:

Function Copy-LastCommand {
 (Get-History)[-1].commandline | clip
}
Filed in: Columns, Tips and Tricks Tags: , , ,

2 Responses to "#PSTip Send the last command executed to clipboard"

  1. Matt says:

    Great stuff. I’m on XP so I didn’t have clip but following a tip from http://weblogs.asp.net/owscott/archive/2006/12/15/clip-saving-command-line-and-powershell-output-directly-to-the-clipboard.aspx I’ve copied clip onto my desktop from a server and it works fine.

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: