# Converting clipboard text content to PowerShell Object

Today I want to show you this super cool [Read-FromClipboard](https://github.com/ztrhgf/useful_powershell_functions/blob/master/Read-FromClipboard.ps1) function for converting **plaintext ** data saved in the clipboard to PowerShell **object**.

![read-fromclipboard_2b.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1638979135469/VzLBFpDSm.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](https://www.powershellgallery.com/packages/ImportExcel/7.2.1) 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](https://github.com/ztrhgf/useful_powershell_functions/blob/master/Read-FromClipboard.ps1), dot source, run.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638962686912/subt7s_-S7.png)

---

# Examples

## Convert table with column names
- **Copy**: table from Excel file or web page
 - You can get some testing tables [here](https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/hierarchy/log-files#BKMK_ClientLogs) 
- **Run**: `Read-FromClipboard`
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638962901943/H2SQrg6zU.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](https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/hierarchy/log-files#BKMK_ClientLogs) 
- **Run**: `Read-FromClipboard -header <column1Name>, <column2Name>` 
 ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638963062730/gRSJ9J9nk.png)
- **Run**: `Read-FromClipboard -headerCount <columnCount>`
 ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638963243971/Wr1yRYTXB.png)
- **Run**: `Read-FromClipboard -header <column1Name> -headerCount <columnCount>`
 ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638963375251/Mi7hWqfVJ.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1638963893188/2YnD6LhBx.png)
- **Run**: `Read-FromClipboard -delimiter ','`
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638963932783/R2wA5GgGh.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1638964000945/08gZpHDst.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1638964229572/9ENM5-3_tr.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1638964116518/kxx6Zx2X2.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 :)
