JP Flouret

Output contents of Windows clipboard in the command line

Today 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.

using System;
using System.Windows.Forms;

namespace jpf {
    class paste {
        [STAThread]
        public static int Main(string[] args) {
            Console.Write(Clipboard.GetText());
            return 0;
        }
    }
}

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.