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

WindowsCmdClipboardCopy Paste

Windows Problem Overview


For example, I can copy a file to the clipboard like this:

clip < file.txt

(Now the contents of file.txt is in the clipboard.)

How can I do the opposite:

???? > file.txt

So that the contents of the clipboard will be in file.txt?

Windows Solutions


Solution 1 - Windows

If you accept to use PowerShell (and not cmd) the you can use Get-Clipboard exactly as you was looking for.

Get-Clipboard > myfile.txt

The adventage 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".

Solution 2 - Windows

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.

Solution 3 - Windows

Clarifying an answer from @Kpym:

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

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

Solution 4 - Windows

To get contents of clipboard

From win cmd:

powershell get-clipboard

or (via a temp file from HTML parser) on cmd:

echo x = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") > temp.vbs
echo WScript.Echo x >> temp.vbs
cscript //nologo temp.vbs

Output may be redirected to file.

Solution 5 - Windows

Using the doskey macro definition feature, you can do:

doskey unclip=(powershell -command "Get-Clipboard") $*

Then (e.g.)

dir/b | clip
unclip | sort/r

Solution 6 - Windows

I have a pair of utilities (from before the Clip command was part of windows) available on this page:

http://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista

There are two utilities in there, Clip2DOS and DOS2Clip. You want Clip2DOS:

Clip2DOS Copyright 2006 Thornsoft Development Dumps clipboard text (1024 bytes) to stdout.
Usage: Clip2Dos.exe > out.txt Result: text is in the file. Limits: 1024 bytes. License: Free, as in Free Beer! http://www.thornsoft.com/dist/techsupport/dos2clip.zip

DELPHI SOURCE INCLUDED!

And hey, here it is (Clip2DOS.dpr) :

{Clip2DOS - copyright 2005 Thornsoft Development, Inc.  All rights reserved.}
program Clip2Dos;

{$APPTYPE CONSOLE}

uses
  Clipbrd,
  ExceptionLog,
  SysUtils;

var
   p : Array[0..1024] of Char;
begin
  try
    WriteLn('Clip2DOS Copyright 2006 Thornsoft Development');
    Clipboard.GetTextBuf(p,1024);
    WriteLn(p);
  except
    //Handle error condition
    on E: Exception do
            begin
              beep;
              Writeln(SysUtils.format('Clip2DOS - Error: %s',[E.Message]));
              ExitCode := 1;    //Set ExitCode <> 0 to flag error condition (by convention)
            end;
  end
end.

Solution 7 - Windows

Pasteboard is another option. It can also work from WSL. First, install via choco:

choco install pasteboard

then the command is simply

pbpaste.exe > file.txt

And that works from cmd and wsl bash.

Solution 8 - Windows

Well, from a million years ago, we did something like this:

type con > filename.txt

... and then you perform your paste operation (Ctrl-v, middle-click the mouse, or choose Edit->Paste from them menu) into the waiting prompt. This will capture the stdin buffer (the console device, named 'con'), and when an end-of-file is received, it will write the contents to the file. So, after your paste, you type 'Ctrl-z' to generate an EOF, and the type command terminates, and the contents of your paste buffer (the clipboard) are captured in 'filename.txt'.

Solution 9 - Windows

There are third party clip commands that work bidirectionally.

Here's one:

    CLIP - Copy the specified text file to the clip board
    Copyright (c) 1998,99 by Dave Navarro, Jr. (dave@basicguru.com)

Solution 10 - Windows

Here is the CLIP program by Dave Navarro, as referred to in the answer by @foxidrive. It is mentioned in an article here: copying-from-clipboard-to-xywrite

A link to the download, along with many other resources is on this page: http://www.lexitec.fi/xywrite/utility.html

Here is a direct link to the download: "DOWNLOAD Clip.exe Copy from and to the clipboard by Dave Navarro, Jr."

Solution 11 - Windows

It may be possible with vbs:

Option Explicit
 
' Gets clipboard's contents as pure text and saves it or open it

Dim filePath : filePath = "clipboard.txt"

' Use the HTML parser to have access to the clipboard and get text content
Dim text : text = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

' to open
If Not IsNull(text) then
Dim WshShell, somestring, txFldr2Open
Set WshShell = WScript.CreateObject("WScript.Shell")

txFldr2Open = "C:\Users"
txFldr2Open = text

somestring = "EXPLORER.exe /e," & txFldr2Open ', /select
WshShell.run somestring
Set WshShell = Nothing
else 
msgbox("Empty")

end if



' Create the file and write on it
msgbox(text)
Dim fileObj : Set fileObj = CreateObject("Scripting.FileSystemObject").CreateTextFile(filePath)
fileObj.Write(text)
fileObj.Close

Solution 12 - Windows

You can use cbecho, a program I wrote in plain C. It will send any clipboard text to stdout, from where you can pipe it to other programs.

Solution 13 - Windows

I am not sure if this command was not supported at that time or not, but it surely does work

clip > file.txt

Solution 14 - Windows

This dirty trick worked for my needs, and it comes with Windows!

notepad.exe file.txt

Ctrl + V, Ctrl + S, Alt + F, X

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - WindowsKpymView Answer on Stackoverflow
Solution 2 - WindowsTedView Answer on Stackoverflow
Solution 3 - WindowsAdam WiseView Answer on Stackoverflow
Solution 4 - WindowsZimbaView Answer on Stackoverflow
Solution 5 - WindowsThomas KnudsenView Answer on Stackoverflow
Solution 6 - WindowsChris ThorntonView Answer on Stackoverflow
Solution 7 - WindowsAdam WiseView Answer on Stackoverflow
Solution 8 - WindowstroyfolgerView Answer on Stackoverflow
Solution 9 - WindowsfoxidriveView Answer on Stackoverflow
Solution 10 - WindowsKevin FeganView Answer on Stackoverflow
Solution 11 - WindowsHakanView Answer on Stackoverflow
Solution 12 - WindowsG. Adam StanislavView Answer on Stackoverflow
Solution 13 - WindowsNephew of StackoverflowView Answer on Stackoverflow
Solution 14 - WindowsdwurfView Answer on Stackoverflow