How can I pipe console output directly to Notepad?

I'm running an application on the command prompt or a Git shell, and I would like to get the output into Notepad, for easier review and editing later on.

I tried the following, but I always get an empty instance of Notepad:

diff file1.txt file2.txt | notepad

I'm aware that I can redirect the output to another file and then open the file in Notepad. I'd like to avoid the extra step, because I'm just used to piping into Vim or less on non-Windows systems.


From what I can tell, there is no way to directly pipe into Notepad.

However, you can pipe into clip and then paste in notepad, like so:

 diff file1.txt file2.txt | clip && notepad

Then just press Ctrl+V in Notepad.


Consider using Vim, or in your case gVim if you prefer a graphical environment.

You can then pipe into it with a single hyphen as an argument, which instructs Vim/gVim to read from standard input.

diff file1.txt file2.txt | gvim -

here's a short Windows program that does it properly (without clobbering the clipboard). It should be adaptable to PowerShell, and I might update this answer if I get the time, but you can also just use that program directly.

Well, how about PowerShell? No need to install another application. Unfortunately, you will need to create a script file somewhere in your PATH...

Short version you can use

If you create a batch file (e.g. ShowInNotepad.bat) with the following contents and place it in your PATH somewhere:

@echo off
clip
powershell -Command $process = Start-Process -PassThru notepad;$SW_SHOW = 5;$sig = '[DllImport("""user32.dll""")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) ^| Out-Null;Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('^^V');

you can then just call echo blah | ShowInNotepad from anywhere!

Note that this does assume that you're using a recent-ish version of Windows (Vista+) and have not disabled PowerShell or uninstalled the .NET framework. In other words, a default Windows installation will work.


Lengthy explanation and alternatives

The easiest way I can think of is to automate the paste (Ctrl+V) action. Which at least one other answer is already doing, but that one uses AHK - you might have better luck getting PowerShell to work in a locked-down corporate environment.

Let's get on with the script, yea?

#start notepad, get process object (to get pid later)
$process = Start-Process -PassThru notepad;

# activate Notepad window
# based on http://stackoverflow.com/a/4994020/1030702
# SW_SHOW activates and shows a window http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx
$SW_SHOW = 5;
$sig = '[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;
[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) | Out-Null;

# send a "Ctrl+V" keystroke to the active window
# from http://stackoverflow.com/a/17851491/1030702
Add-Type -AssemblyName System.Windows.Forms;
[System.Windows.Forms.SendKeys]::SendWait('^V');

It's pretty straightforward, so I won't bother explaining the script more than the comments already do.

Usage

To use it, you just need to place the script in a .ps1 file (e.g. ShowInNotepad.ps1), place it somewhere in your PATH and then call powershell ShowInNotepad.ps1 after placing the text you want to display in the clipboard.

Example:

echo blah | clip && powershell ShowInNotepad.ps1

Unfortunately, executing PowerShell scripts can sometimes be difficult (execution policies and all). Therefore, I've condensed this script to a one-liner you can call directly from the Command Prompt, or even place into a batch file:

powershell -Command $process = Start-Process -PassThru notepad;$SW_SHOW = 5;$sig = '[DllImport("""user32.dll""")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) ^| Out-Null;Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('^^V');

If you create a batch file (e.g. ShowInNotepad.bat) with the following contents and place it in your PATH somewhere:

@echo off
clip
powershell -Command $process = Start-Process -PassThru notepad;$SW_SHOW = 5;$sig = '[DllImport("""user32.dll""")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) ^| Out-Null;Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('^^V');

you can then just call echo blah | ShowInNotepad from anywhere!