Output contents of Windows clipboard in the command line
27 November 2014Today I found myself needing a command line utility that would output the contents of Windows clipboard to standard output. I was seriously disappointed with the lack of such tool in Windows so I wrote one in one line of code.
OK, that’s more than one line of code but it is mostly boilerplate. The meat
of the problem is solved in a single line of code (line 8) with
Console.Write(Clipboard.GetText())
.
To pipe the contents of the clipboard to another command (e.g. grep):
paste | grep pattern
To save the contents of the clipboard to a file:
paste > file.txt
You can compile paste.cs with a single command on any Windows machine without the need to install any extra tools. Just run this command:
%SystemRoot%\Microsoft.NET\Framework\v3.5\csc /o paste.cs
That’s it, enjoy.