PowerShell script to format data in Clipboard

Rich Rumble
Rich Rumble used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sam JacobsDirector of Technology Development, IPM

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
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
Top Expert 2015
Commented:
Your last example is wrong, input needs to have aaaa, not aaa ;-).
Because you want to have a certain order, we need to process in a loop:
# "keys" to keep, in desired order
$keys = 'eeee', 'aaaa', 'dddd'
# only keep interesting content having value
$content = get-clipboard | Select-string "^($($keys -join '|')):.+"
# now create sorted output
Set-Clipboard -value ($keys | foreach { $content | Select-String "^$_`:" } )

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Rich RumbleSecurity Samurai
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
Top Expert 2006

Author

Commented:
Thanks!
Rich RumbleSecurity Samurai
Top Expert 2006

Author

Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial