62

On Mac OS X and GNOME on Linux, there are commands that can be used to open files from the command line in their associated GUI editors: open and gnome-open, respectively. Is there any command like this for Windows?

  • @badp: I mentioned "GNOME on Linux" not just Linux. – Wuffers Feb 17 '11 at 2:33
  • Whoops, sorry. – badp Feb 17 '11 at 2:34
  • @badp: No worries. ;) – Wuffers Feb 17 '11 at 2:35
  • xdg-open is the more universal Linux example, if anybody cares – furicle Feb 21 '18 at 20:08
58

If you are currently in the command prompt and have a file called test.png and , which are located in c:\test you can do the following:

If you are at the directory (so should say c:\test>) just type:

test.png

which would open test in the default png picture editor.

If the files name contains spaces, then simply enclose the file name within " "

 "this image.png"

You can alternatively type:

c:\test\test.png

which will open the file no matter where you currently are.

Finally, you can pass the picture to another program. For example, if you have an image editor called imageedit.exe and it supports opening files through command lines (and if the program is pathed/accessible or you are in it's current directory), you can type the following:

 imageedit c:\test\test.png
  • Just curious: is there also a way to close files or programs or windows through command prompt? – Miles Johnson May 13 '17 at 2:29
  • @MilesJohnson only by calling other tools such as taskkill.exe – William Hilsum May 29 '17 at 8:40
  • 1
    works from cmd, but not from PowerShell or Git Bash - explorer is the more universal way – furicle Feb 21 '18 at 20:10
49

If it is a registered extension, you can use "start" as in

start WordDoc.doc
  • 6
    Good answer for those using Bash or another shell. – Yves Junqueira Mar 18 '16 at 2:00
  • This is actually the correct answer. It even opens the default browser if you supply a URL. – kumarharsh Dec 1 '16 at 9:12
  • 1
    When I enclose the name in quotes, it instead opens a new shell window for some reason. – Erhannis Apr 18 '17 at 15:17
  • 3
    As Steven Digby mentions below start's first parameter is the window title so something like start "" WordDoc.doc would work – Tony Brix Jan 10 '18 at 20:32
  • start does different things depending on the shell you use. With Powershell, it invokes Start-Process With cmd it invokes the tradional start With Git Bash, it invokes the cmd Start The cmd Start does not handle network locations. The PowerShell version will, as will 'explorer ' as detailed below. – furicle Feb 21 '18 at 20:05
10

On DOS command-line explorer "<PATH>" will open the file path with Windows default associated programs. This will also handle all URIs ( http:,https:,ftp: ) and other file protocols defined in Windows Operating System. If the file or protocol is not associated with any program then an Open With dialog will show up. If file is not present then default My Documents folder will open up. It can also open executable files ( EXE, BAT files) and shell namespace paths.

Examples

explorer "http://www.google.com"- will open http://www.google.com in windows default browser.

explorer "file:///C:\temp\" will open temp directory if present

explorer "file.txt" will open file.txt on the current directory path .i.e. %CD% path

explorer ::{645ff040-5081-101b-9f08-00aa002f954e} will open RecycleBin.

You can refer about explorer's other useful command-line switches here

5
powershell -c "folder\childfolder\file.txt"

Source: https://technet.microsoft.com/en-us/library/ee176882.aspx

Simple and versatile.

  • Looks good to me. – DavidPostill Mar 9 '16 at 21:04
  • @DavidPostill would you consider my answer as useful? – Eric Mar 9 '16 at 21:05
  • Yes. Have a +1 ;) – DavidPostill Mar 9 '16 at 21:13
1

The first parameter of Start is a window title, so if you have a space in the file name, and you type

Start "My File.txt"

you'll get a command line window with "My File.txt" as the title. To get around this use a dummy title,

Start "my title" "My File.txt"

Depending on the file and what application is opened there probably won't be a window to see anyway.

1

If you are in PowerShell (at the PS (current-directory)>  prompt), and you want to open a file in the current directory, you may try this .\myfile.ext.  If you are in the Command Prompt, you can accomplish the same result by typing

powershell -c .\myfile.ext

(You must include the .\, as PowerShell doesn’t load files from the current location by default.)  Or you can provide a directory name (relative or absolute) if the file isn’t in the current directory.

For the first scenario — if you are in PowerShell — if the filename contains space(s), enclose it in quotes (either kind) and precede it with the & symbol:

PS C:\Users\myusername\Pictures> &".\funny cat.jpg"

(You may add a space after the & for readability, if you prefer readability, and you may use / instead of \.)  I don’t know how to make this work in the second scenario (in which you are running powershell -c from Command Prompt) if the file or directory name contains space(s) — quotes don’t seem to help.

0

Specific executable with specific file:

Git Bash Script On Windows 10 :


SHORTCUTS_MAIN_START.sh :

    FILE_PATH="C:\DEV\REPO\GIT\AHK03\SHORTCUTS\SHORTCUTS_MAIN.ahk"
    EXEC_PATH="C:\DEV\REPO\GIT\AHK03\AHK_ROOT\AutoHotkeyU64.exe"

    $EXEC_PATH $FILE_PATH

This example opens the file denoted by $FILE_PATH with the executable denoted by $EXEC_PATH. If this doesn't work for you, try converting the single slash ( "\" ) into double slashes ( "\\" ) for the paths.

-1

This may come a bit late, but the correct command for editing a file name in Windows 7 is "write file_name"

This should open up the default text editor and you should be able to edit the file easily

Edit: It seems to open only Wordpad. For me that was the default text editor.

  • Nice trick. It does not start the default text editor however. My default editor for .txt files is Crimson editor, but this command opens Wordpad. – Jan Doggen Jul 31 '14 at 18:30
  • Yes, it does so for me too. Wordpad was default for me. Thanks for the update! Regardless, hope this helps the original poster. – Coder Aug 1 '14 at 23:01
  • 2
    write is short for write.exe which is the executable file of wordpad. It's stored in C:\windows which is on the %PATH%, therefore it's just the regular way to open a file with wordpad. – amenthes May 12 '17 at 8:25
-3

In the Windows command prompt, you can run

edit [file_name]

in order to view batch files/logs/text files etc. This command requires QBASIC.EXE, which is by default present in Windows.

See here for other useful MS-DOS commands.

  • 2
    Doesn't seem to work on Windows 7 64-bit, neither edit nor qbasic.exe are recognised. Also, this only works with plain text files. – Indrek Sep 10 '12 at 7:06
  • @Indrek, Not recognized on Win8 too. – Pacerier Mar 20 '15 at 7:31
  • Modern Windows command line is NOT DOS. – oldherl Jun 11 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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