Converting clipboard text content to PowerShell Object

Converting clipboard text content to PowerShell Object

Today I want to show you this super cool Read-FromClipboard function for converting plaintext data saved in the clipboard to PowerShell object.

read-fromclipboard_2b.gif

I am quite often in a situation when I need to further process data from a web page table or some monitoring email that ended in my inbox. I mean that I have to convert such plaintext into a PowerShell object so it can be easily sorted, transformed, etc.

There are ways how to get such data into your PowerShell console for further processing, but it can be cumbersome. Because you often have to download, modify, convert, ... the file to be able to import it into the console.

What I wanted was an easy-to-use tool that allows me to just copy the data I need to the clipboard and use them in my PowerShell console straight away without other disturbance.

My function is inspired by Read-Clipboard function from ImportExcel module so kudos to the author for this great idea!


Main Read-FromClipboard function features

  • converts XML plaintext from clipboard to PowerShell XML object
  • converts JSON plaintext from clipboard to PowerShell object
  • converts CSV-like plaintext from clipboard to PowerShell object
  • supports defining own headers
  • supports numbered headers (or combination of own and numbered)
  • supports usage of regex delimiter

How to use it

Download, dot source, run. image.png


Examples

Convert table with column names

  • Copy: table from Excel file or web page
    • You can get some testing tables here
  • Run: Read-FromClipboard image.png

Convert table without column names

  • Copy: table from Excel file or web page (without column names line)
    • You can get some testing tables here
  • Run: Read-FromClipboard -header <column1Name>, <column2Name> image.png
  • Run: Read-FromClipboard -headerCount <columnCount> image.png
  • Run: Read-FromClipboard -header <column1Name> -headerCount <columnCount> image.png

Convert single-column string to array

  • Copy:
    N4-01-NTB
    NG-06-NTB
    NG-07-NTB
    NG-18-NTB
    NG-30-NTB
    
  • Run: Read-FromClipboard -headerCount 1

Convert text where delimiter isn't TAB (it is for example comma)

  • Copy:
    company, type, year
    ford, focus, 2008
    ferrari, f40, 2015
    porshe, cayen, 2012
    
  • Run: Read-FromClipboard image.png
  • Run: Read-FromClipboard -delimiter ',' image.png

Convert text where delimiter is regular expression

  • Copy:
    2002 89           144588
    5207   195   286136
    426      19     6552
    
  • Run: Read-FromClipboard -delimiter "\s+" -regexDelimiter -headerCount 3 image.png

Convert XML text

  • Copy:
    <?xml version="1.0" encoding="UTF-8"?>
    <!--Settings used with MSEndpointMgr Driver Automation Tool-->
    <!--URL's are maintained externally - USE AT YOUR OWN RISK. NO SUPPORT PROVIDED -->
    <OEM Version="1.3">
      <Manufacturer Name="Dell" >
          <Link Type="DownloadList" URL="https://downloads.dell.com/published/Pages/index.html"></Link>
          <Link Type="DownloadBase" URL="https://downloads.dell.com"></Link>
          <Link Type="DriversList" URL="https://en.community.dell.com/techcenter/enterprise-client/w/wiki/2065.dell-command-deploy-driver-packs-for-enterprise-client-os-deployment"></Link>
          <Link Type="BaseURL" URL="https://en.community.dell.com"></Link>
          <Link Type="BIOSUtility" URL="https://dl.dell.com/FOLDER07078356M/7/Flash_Ver3.3.11_ZPE.exe"></Link>
          <Link Type="XMLCabinetSource" URL="https://downloads.dell.com/catalog/DriverPackCatalog.cab"></Link>
          <Link Type="CatalogSource" URL="https://downloads.dell.com/catalog/CatalogPC.cab"></Link>
      </Manufacturer>
    </OEM>
    
  • Run: Read-FromClipboard image.png

Convert JSON text

  • Copy:
    {
      "files.defaultLanguage": "powershell",
      "files.trimTrailingWhitespace": true,
      "files.trimFinalNewlines": true,
      "files.associations": {
          "*.ps1": "powershell",
          "*.wsb": "xml"
      }
    }
    
  • Run: Read-FromClipboard image.png

Summary

Working with clipboard content instead of saving it to some temp files is addictive and cool. Btw if you miss some nice feature let me know and I will add it :)

Did you find this article valuable?

Support Ondrej Sebela by becoming a sponsor. Any amount is appreciated!