Open file from the command line on Windows

65

13

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?

Wuffers

Posted 2011-02-17T02:30:46.743

Reputation: 16 645

xdg-open is the more universal Linux example, if anybody cares – furicle – 2018-02-21T20:08:07.143

@badp: I mentioned "GNOME on Linux" not just Linux. – Wuffers – 2011-02-17T02:33:05.713

Whoops, sorry. – badp – 2011-02-17T02:34:17.693

@badp: No worries. ;) – Wuffers – 2011-02-17T02:35:09.830

Answers

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

William Hilsum

Posted 2011-02-17T02:30:46.743

Reputation: 111 572

Just curious: is there also a way to close files or programs or windows through command prompt? – Miles Johnson – 2017-05-13T02:29:43.903

@MilesJohnson only by calling other tools such as taskkill.exe – William Hilsum – 2017-05-29T08:40:50.967

1works from cmd, but not from PowerShell or Git Bash - explorer is the more universal way – furicle – 2018-02-21T20:10:58.690

52

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

start WordDoc.doc

Dennis

Posted 2011-02-17T02:30:46.743

Reputation: 5 768

6Good answer for those using Bash or another shell. – Yves Junqueira – 2016-03-18T02:00:49.213

This is actually the correct answer. It even opens the default browser if you supply a URL. – kumarharsh – 2016-12-01T09:12:04.640

1When I enclose the name in quotes, it instead opens a new shell window for some reason. – Erhannis – 2017-04-18T15:17:22.687

3As Steven Digby mentions below start's first parameter is the window title so something like start "" WordDoc.doc would work – Tony Brix – 2018-01-10T20:32:05.117

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 – 2018-02-21T20:05:44.473

11

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

lalthomas

Posted 2011-02-17T02:30:46.743

Reputation: 267

5

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

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

Simple and versatile.

Eric

Posted 2011-02-17T02:30:46.743

Reputation: 151

Looks good to me. – DavidPostill – 2016-03-09T21:04:07.547

@DavidPostill would you consider my answer as useful? – Eric – 2016-03-09T21:05:33.453

Yes. Have a +1 ;) – DavidPostill – 2016-03-09T21:13:32.380

2

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.

Steven Digby

Posted 2011-02-17T02:30:46.743

Reputation: 31

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.

andrey.shedko

Posted 2011-02-17T02:30:46.743

Reputation: 111

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.

J MADISON

Posted 2011-02-17T02:30:46.743

Reputation: 219

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

Coder

Posted 2011-02-17T02:30:46.743

Reputation: 1

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 – 2014-07-31T18:30:48.140

Yes, it does so for me too. Wordpad was default for me. Thanks for the update! Regardless, hope this helps the original poster. – Coder – 2014-08-01T23:01:29.940

2write 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 – 2017-05-12T08:25:25.250

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

phoenix079

Posted 2011-02-17T02:30:46.743

Reputation: 1

@Indrek, Not recognized on Win8 too. – Pacerier – 2015-03-20T07:31:33.090

Modern Windows command line is NOT DOS. – oldherl – 2019-06-11T11:16:30.553

2Doesn'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 – 2012-09-10T07:06:59.253