How can you get the clipboard contents with a Windows command?

You can use the paste.exe software in order to paste text just like you are describing.

http://www.c3scripts.com/tutorials/msdos/paste.html

With it you can do:

paste | command

to paste the contents of the windows clipboard into the input of the specified command prompt

or

paste > filename

to paste the clipboard contents to the specified file.


If it would be acceptable to use PowerShell (and not cmd), then you can use Get-Clipboard exactly as you were looking for.

Get-Clipboard > myfile.txt

The advantage of this method is that you have nothing to install.

Note: In place of clip you can use Set-Clipboard that has more options.

Note 2: If you really want to run it from cmd, you can call powershell as in the following example powershell -command "Get-Clipboard | sort | Set-Clipboard".


Clarifying an answer from @Kpym:

powershell -command "Get-Clipboard" > file.txt

This directly answers the question without using a 3rd party tool.