Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-MdtClipboard {
- <#
- .SYNOPSIS
- Obtains the contents of the clipboard.
- .DESCRIPTION
- Obtains the contents of the clipboard. By default, each line will be a separate object and blank lines will be omitted. Use the -SpreadSheet switch if you are copying a region from an excel sheet.
- .PARAMETER IncludeEmptyLines
- When returning an array of objects, ignore empty lines on the clipboard.
- .PARAMETER TextBlock
- Treat the clipboard as a single string. Default behavior is to return an array of objects delimited by linebreaks, a la Get-Content.
- Brief parameter description
- .PARAMETER SpreadSheet
- Parses the clipboard as a tab-delimited CSV file. This is the default format when copying data out of Excel. Unless the header parameter is used, the first line is treated as the column headers.
- .PARAMETER Header
- Specify text array to use as column headers when interpreting clipboard contents as a CSV.
- .EXAMPLE
- Get-MdtClipBoard -SpreadSheet -Header "Name","Email"
- Return the copied Excel cels as an array of objects, using a custom header to name the columns.
- #>
- [CmdletBinding()]
- Param(
- [switch]$IncludeEmptyLines,
- [switch][alias("Block","TB")]$TextBlock,
- [switch][alias("SS")]$SpreadSheet,
- [array]$Header
- )
- Begin {}
- Process {
- Add-Type -Assembly PresentationCore
- $out = [Windows.Clipboard]::GetText()
- IF (!($TextBlock)) {
- $out = $out -replace "`r", '' -split "`n"
- IF ($SpreadSheet) {
- IF ($header) {
- $Out = ConvertFrom-Csv $out -Delimiter "`t" -Header $header
- }
- ELSE {
- $Out = ConvertFrom-Csv $out -Delimiter "`t"
- }
- }
- IF (!($IncludeEmptyLines)) {
- $out = $out -ne ''
- }
- }
- ELSEIF ($IncludeEmptyLines -or $SpreadSheet -or $header) {
- Write-Warning "In TextBlock mode, all other parameters are ignored."
- }
- Write-Output $out
- }
- END {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement