Created
January 31, 2014 03:56
-
-
Save gpduck/8726386 to your computer and use it in GitHub Desktop.
A function that pulls your clipboard data as CSV and converts it to objects (try copying a block from Excel).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ClipboardAsCsv { | |
param( | |
[Parameter(Mandatory=$false)] | |
[char]$Delimiter, | |
[Parameter(Mandatory=$false)] | |
[ValidateNotNullOrEmpty()] | |
[string[]]$Header | |
) | |
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") > $null | |
$ms = [Windows.Forms.clipboard]::getdata([Windows.Forms.DataFormats]::CommaSeparatedValue) | |
if($ms) { | |
$sr = New-Object IO.StreamReader($ms) | |
try { | |
$Lines = while(!$sr.EndOfStream) { | |
$sr.ReadLine() | |
} | |
$Lines | ?{![string]::IsNullOrEmpty($_) -and $_ -ne "`0"} | ConvertFrom-Csv @PSBoundParameters | |
} finally { | |
if($sr) { | |
$sr.Close() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment