Skip to content

Instantly share code, notes, and snippets.

@gpduck
Created January 31, 2014 03:56
Show Gist options
  • Save gpduck/8726386 to your computer and use it in GitHub Desktop.
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).
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