Clip-ToArray for PS V3 ISE

We’ve probably all done it – copied and pasted a list of something (user names, email addresses, ip addresses, etc.) from a spreadsheet, email, text document, web page, etc. into a here-string in an IDE and then split it to get an array.  It’s quick, and easier to look at than a csv string.

Playing with the new ISE in the PS V3 CTP, I came up with this.  Run this to add a ClipToArray add-on to your ISE. Once it’s added, just copy some list to your clipboard, set your variable and then run the addon and it will add the code at the current cursor position to create a here-string from the clipboard contents and split it out into an array.  There’s an optional filter to exclude any lines that start with # that’s commented out by default. If you don’t need it you can just leave it commented, or optionally just delete the line.

function clip-toarray{
$sb = @"
$([windows.forms.clipboard]::GetText() -replace '[`n\s*]+$','')
'@).split("``n") |
#where {$_ -notmatch '^#'} |
foreach {`$_.trim()}`n
$ise_current_file = $psise.CurrentFile


Using a list of the Scritping Guys 2011 Archives copied from his blog page, the output looks like this:

$archives = (@’
December 2011 (31)
November 2011 (30)
October 2011 (31)
September 2011 (30)
August 2011 (31)
July 2011 (31)
June 2011 (30)
May 2011 (31)
April 2011 (97)
March 2011 (41)
February 2011 (28)
January 2011 (31)
‘@).split(“`n”) |
#where { -notmatch ‘^#’ |
foreach {$_.trim()}


4 responses to “Clip-ToArray for PS V3 ISE

  1. you’re missing a } in your where!
    nice work though, very useful way to get data in quickly.

    • Fixed!

      Thanks for catching that!

      Considering working on another one for typecasting imported csv data, using the new features of OGV as a menu system to select the properties to be typed, and the type to use.

  2. not sure if the sourcecode tags will help this, but when you copy and paste this code it buffers it with whitespace which screws up the creation of the fucntion and the output of it.
    also, how about a way to do something like this
    $myarr = clip-toarray
    so that it does ALL the work 🙂

    great job!

  3. Pingback: PSReadLine: A Better Line Editing Experience for the PowerShell Console | Keith Hill's Blog

Leave a Reply

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

You are commenting using your 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