Saturday, June 20, 2020

power(shell), corruption & lies: Tee-Object and the clipboard

Really useful powershell for me this morning. I wanted to run some commands, look at the output, and capture the output to the clipboard so I could paste it elsewhere.

Rather than run my command and then use the mouse in the comand window to copy the output to my paste buffer, like some kind of animal, I wanted to have a full powershell solution.

FOOBAR | Tee-Object -Variable c; Set-Clipboard $c

How it works:
  1. FOOBAR is the name of the command (or series of piped commands) that I want to run that write to the console.
  2. Tee-Object -Variable c takes the output from FOOBAR and passes it forward (in this case to the console output) and to the variable $c.  

    Tee-Object is kind of like a plumbing tee where data is sent in two directions. It's normally used to show output on the console and to redirect to a file. 

    Note that you say -Variable c not -Variable $c. 
  3. Set-Clipboard $c sets the contents of the paste buffer with the value in $c.

No comments:

Post a Comment