74

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?

2
  • Solution without 3rd party software here: stackoverflow.com/a/15747067/1683264
    – rojo
    Commented May 12, 2017 at 12:14
  • I know I'm late to the party, but I think my answer (see below) is probably the closest to the intent of the question.
    – troyfolger
    Commented May 27, 2021 at 17:05

13 Answers 13

56

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

0
36

Clarifying an answer from @Kpym:

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

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

3
  • Since DOSKEY macros and batch scripts don't work reliably in a pipe, it seems like your only recourse for processing clipboard text in a proper pipeline (à la Unix, through programs like sort, more, findstr that read from stdin) is to use PowerShell. If you're a gentle soul who prefers CMD.EXE and doesn't thrive in PowerShell, you can construct the entire pipe as a PowerShell command, like this: powershell -command "get-clipboard | findstr STRING | sort | more".
    – Kevin E
    Commented Aug 28, 2019 at 0:48
  • It seems that the redirection outside of PowerShell corrupts encoding (if the file is not pure ASCII). Redirecting within PowerShell helps. Though you have to specify the encoding in Windows PowerShell, as it defaults to UTF-16, what one hardly ever wants. In PowerShell Core, it's as easy as pwsh -command "Get-Clipboard > file.txt", as PowerShell Core defaults to UTF-8. Commented Apr 23, 2021 at 14:57
  • 2
    fwiw a powershell2 (windows7) compatible alternative is powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"
    – hanshenrik
    Commented Feb 7 at 23:12
31

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.

8
  • 14
    so there isn't anything that comes with windows?
    – Matt
    Commented Jul 23, 2013 at 20:51
  • 6
    clip.exe, which I mention in the question, comes with Windows.
    – Matt
    Commented Jul 24, 2013 at 14:22
  • 2
    Don't expect miracles, an image or spreadsheet copied to a clipboard isn't going to look that good in a text file. Which is of course the reason why there isn't a standard command for this, too many accidents. Commented Jul 24, 2013 at 16:04
  • 9
    @HansPassant Oh, I'm angry. It is not a reason! How many kittens died because of these accidents?
    – kan
    Commented Feb 19, 2016 at 16:58
  • 1
    This seems a bit too simplified, given the fact the clipboard can contain loads of stuff in parallel. Just copying a file in File Explorer in Win10 gives me 7 different byte stream blocks in the clipboard. None of which are the actual file, btw.
    – Nyerguds
    Commented Apr 15, 2019 at 11:40
14

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.

6

Using the doskey macro definition feature, you can do:

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

Then (e.g.)

dir/b | clip
unclip | sort/r
1
  • 4
    Nice for usage on the command line, but Doskey macros don't work in batch files. (no critique, just to note)
    – Stephan
    Commented Feb 1, 2020 at 10:04
6

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

1
  • 2
    I see. That's similar to cat > filename.txt (then paste and Ctrl+D) on *nix. It's been so long that can't remember if that would have worked in my original use case, but useful to know. Thanks.
    – Matt
    Commented Aug 26, 2021 at 17:17
5

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.

4

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.
1
  • Oh just what I needed, since, you know, I have a Delphi compiler and no paste utility
    – ardnew
    Commented Feb 9, 2022 at 23:16
2

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. ([email protected])
2
  • 4
    How does one get this?
    – Mark Deven
    Commented Jan 1, 2019 at 23:12
  • That's a 20 year old program and it doesn't pipe output to STDOUT. It only copies from a file or STDIN and pastes only into a file.
    – pbarney
    Commented May 28, 2020 at 19:48
0

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

0

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
0

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.

1
  • 2
    Please do not post link only answers, you need to at least use crucial parts of the link in the answer. Link's can change leaving the answer useless.
    – Gerhard
    Commented May 10, 2021 at 17:39
-10

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

notepad.exe file.txt

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

4
  • 7
    Very funny. Unfortunately, that is very slow with very large clipboards.
    – Matt
    Commented Mar 27, 2015 at 17:39
  • 1
    Hmm, but what happens if I accidentally reboot my system in the process? Alt + F4 is very dangerous. Commented Sep 27, 2016 at 6:21
  • @MateenUlhaq If the taskbar has focus it may pull up the shutdown menu but Alt + F4 will not restart without an additional click or keypress.
    – dwurf
    Commented Nov 9, 2016 at 4:21
  • @MateenUlhaq Alt + F + X is safer.
    – jpaugh
    Commented Aug 30, 2017 at 14:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.