Link to home
Start Free TrialLog in
Avatar of Rich Rumble
Rich RumbleFlag for United States of America

asked on

PowerShell script to format data in Clipboard

I'd like to have a PowerShell script that reads the contents of my clipboard, parses and formats the data, then re-writes the clipboard so I can paste it in formatted.
The entire process is, copy data to clipboard, press hot-key shortcut to the script (ctrl+alt+q for example), script re-writes the clipboard, then I paste clipboard to destination.

I have data like this:
---------------
INPUT:
IP Address: 10.10.10.10
Mac Adress: ab-cd-12-34-56-78-90
User Name: Grace Jones
Domain: Forrest-1
OU: Sales and Engineering
Department: Sales
Manager: Gus Fring
Location: ABQ
Computer Name: AsdfNiner5er
---------------
The output I'd like only certain fields, in a certain order, not all fields are present with each copy, sometimes Mac-Address: isn't there, or IP: isn't populated. Basically you can not assume that any one of the lines above will be present and or not populated. "IP: x.x.x.x" may be missing entirely, or "Mac-Address: " might not be populated after the colon. When they are not present, and or not populated, I don't want to see them in the output either.
Output:
    IP: 10.10.10.10
    Mac-Address: AB-CD-12-34-56-78-90
    User Name: Grace Jones
-----------
There are dozen's more input fields , and a few more the output, but that's the basic's. I figure "get-clipboard" and "set-clipboard" will do the trick nicely, I just have no idea about the parsing/formating.
Thanks!
-rich
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Rich,

Save this as a .ps1 and assign a shortcut key to it:
$clipboard = @(Get-Clipboard)
$newCB = @()
foreach ($cb in $clipboard) {
   $aCB = @($cb.Split(":"))
   if ($aCB.Count -gt 1) {
	$newCB += $cb
   }
}
Set-Clipboard -Value $newCB

Open in new window

Avatar of Rich Rumble

ASKER

I like this so far, now for the formatting, I want to throw away most of the lines of input, and only have a select few put back into the clipboard (output).
And I still want what is on both sides of the colon, the entire line is fine, I just want to output certain lines, and in a certain order, rather than just alphabetically or numerically. I want to format the input into a certain output. Does that make sense? My example I don't think made that clear :) I see that now.
-rich
INPUT
aaa:1111
bbb:2222
cccc:3333
dddd:4444
eeee:5555
fffffff:66666
------
OUTPUT:
eeee:5555
aaaa:1111
dddd:4444
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am going to accept Qlemo's solution, I had to add a space before the colons in lines 4 and 6 to match my real data :)
My next question will build off of this one, running a command like "nslookup" when what's on the right of the colon for "IP Address : " is not null, and or when I want to open a another program based on what's on the right of the colon. I'll post that follow on Q here too.
-rich
Thanks!