We help IT Professionals succeed at work.

PowerShell script to format data in Clipboard

Rich Rumble
Rich Rumble asked
on
176 Views
1 Endorsement
Last Modified: 2018-10-29
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
Comment
Watch Question

Sam JacobsCitrix Technology Professional / Director of TechDev Services, IPM
CERTIFIED EXPERT

Commented:
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

Rich RumbleSecurity Samurai
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:
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
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Rich RumbleSecurity Samurai
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:
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
Rich RumbleSecurity Samurai
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:
Thanks!
Rich RumbleSecurity Samurai
CERTIFIED EXPERT
Top Expert 2006

Author

Commented:

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions