What is the difference between opening application by cmd/ps or by click? - windows

I try to copy mozilla firefox bookmarks/passwords into a new windows profile during the login either by cmd or powershell, without copying the whole firefox profile: The script opens firefox (to create a profile), closes firefox and overwrite the created bookmarks with the old ones.
Problem:
The user opens firefox and in this moment firefox deletes the copied bookmarks/passwords and creates new ones.
In case the user opens and closes firefox manually, bookmarks/passwords can be copied by script and everything is fine.
Obviously there is a difference how Windows is handling files when opening by click or by cmdlet / powershell.
Commands I tried to open firefox:
[PS] Start-Process "C:\Program Files\Mozilla Firefox\firefox.exe" -PassThru
[CMD] start "" "C:\Program Files\Google\Chrome\Application\chrome.exe"
[CMD] "C:\Program Files\Mozilla Firefox\firefox.exe"
How can I imitate the opening of the application as if it is opened manually by the user?
Regards

You technically can't statically analyze how Explorer opens something because of shell extensions but that is unlikely to apply to .exe files so let us ignore that for now.
How does cmd.exe and batch files launch a process? CreateProcess.
How does Explorer launch a file? ShellExecuteEx most of the time.
That being said, in the case of .exe files, ShellExecuteEx might append to %path% and some other minor things but in the end it is going to end up calling CreateProcess to start the actual process.
I believe your problem is somewhere else, not in the difference between cmd.exe and Explorer.
The files are locked because someone (Firefox still running?) has a open handle to the bookmarks?
It could be a problem related to wrong the current directory? The current directory is locked by Windows so you can't replace it by deleting it.
Firefox running update task started by the task scheduler during login at the exact same time as your login script?
You can get PowerShell to use ShellExecuteEx by setting UseShellExecute to $true before starting the process but according to this it is already true by default (assuming your PassThru parameter does not force it to false).

Related

Open a file with a specific program using cmd

I want to open a HTML file using cmd, located in the autostart folder using internet explorer, even tho for example Firefox is selected for standard browser.
I already navigated to the autostart folder.
How can I do that?
Or what is the command for that?
%ProgramFiles%\Internet Explorer\iexplore.exe" file://path_to_file
Refer to the command line options for IE11 for details of options that you might need

Windows CMD: How to create symbolic link to executable file?

My goal is to add a few executables to my PATH (for example, chrome), so that I can call
> chrome
from the command prompt and it will launch Chrome.
I know I could add Chrome's containing directory to my path (set PATH=%PATH%<chrome_path_here>;), but since I have a few executables I want to add, I'd rather make a new bin directory that contains symbolic links to the actual executables and just add that single directory to my PATH.
The Chrome executable is located at
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
So I tried
> mklink chrome.exe "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
That successfully creates a symbolic link for the files (says so in output, and upon examining with > dir). I know my PATH is set up correctly, b/c when I run > where chrome it finds my new symbolic link.
However, when I try to execute chrome with my new link, nothing happens. A new empty window should appear, but nothing happens. No error message in the command prompt or anything.
What am I doing wrong? Am I misunderstanding symlinks in Windows? This is the approach I use in Linux all the time, but I'm new to Windows Cmd.
Thanks!
Most programs will not run from places other than they install location - which is exactly what happens when you try to run it from symlink.
It would be much easier to create CMD/BAT files in that folder with matching names which will launch programs from locations you want:
REM chrome.cmd
start /b cmd /c "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %*
With Windows 7 I confirm that symlinks do not work, are simply ignored as reported in the original question.
As Harry states in his comment, shortcuts do work, and to me are simpler and easier than writing a separate script for each new command I want to enable under CMD.
He states that you need to add .lnk to your PATHEXT variable in order to do this. I affirm that this does work, and with .lnk added to PATHEXT I can simply enter the name portion of the shortcut to run the command. For example if my shortcut is named "sublime.lnk" and PATHEXT includes .lnk, I can execute the link with the simple command "sublime". Nice!
As an alternative I found that PATHEXT need not be modified if I simply type in the full name of the shortcut, including the .lnk, at my CMD prompt. E.g., I created a shortcut named "sublime.lnk" under %HOMEPATH%/bin, pointing to "C:\Program Files\Sublime Text 2\sublime_text.exe".
Now by placing %HOMEPATH%\bin in my %PATH% I can run sublime via the command "sublime.lnk".
Either of the above are the best way I know of giving access to various commands from around Windows' filesystem from a CMD prompt. I'm not a Windows expert though, and welcome a better or more standardized solution to this problem.
P.S.: I just found out the hard way that you need to ensure the "Start in:" property of any shortcut you use in this fashion is blanked out, or your program will not start in the directory you invoke the shortcut from.
P.P.S.: On a related note, I discovered how to run Windows Explorer (or its replacement) on the directory your CMD session is logged in to: start ..

How to launch a program from perl?

How do i launch firefox from perl? i just need to launch the browser so WWW::Mechanize::Firefox can manipulate it. Searching around stackoverflow ive seen a few solutionsl like system('start cmd.exe /k $cmd) where $cmd is arguments to throw as input once cmd is started.
However, these have not helped me to solve my problem at all.
solutions ive tried
system("start cmd.exe /k start firefox");
system("firefox");
system("cmd","start","firefox");
system("cmd start firefox");
Basically a lot of the alternatives ive found, but i could not launch Firefox browser at all.
You're on the right track. Your second line is almost correct. If firefox is not in your PATH environment variable, you need to supply the complete path.
Click on the Firefox icon on your desktop, open the properties and check where the firefox executable is located. Then use that with your system call.
For me, it looks like this (the ' are for Perl's string, the " are for the Windows shell, because the path has spaces in it):
system('"C:\Programme\Mozilla Firefox\firefox.exe"');
You can test it by opening a new command line (win + r, cmd), cding to the directory where your Perl program is run from, and just entering the command:
C:\Dokumente und Einstellungen\simbabque>"C:\Programme\Mozilla Firefox\firefox.exe"
It will not print anything, but just open a new Firefox window after a couple of seconds. So you'd probably need to hold your program execution in Perl while the browser is starting up.
WWW::Mechanize::Firefox will launch firefox for you but you can use
system 1, qq{$ENV{PROGRAMFILES}\\Mozilla Firefox\\firefox.exe}

How to restore bat file execution from explorer on XP

some bat files need to be launched at the start of the session, but they don't.
Actually, when trying to launch one from the explorer, it opens the Open with window instead of just runnig.
However, the bat files still run correctly from the command prompt.
I bet it has been caused by a virus, but the antivirus did not detect anything unfortunately.
Any idea?
Use the Open With command, select cmd.exe, then before you hit OK, check the box that says "always use this program to open this type of file" or something similar.
If cmd.exe is not in the choices, browse to c:\windows\system32, and cmd.exe should be in there.
You can re associate the extension by downloading the reg file for batch from the link below which should solve your problem.
http://www.dougknox.com/xp/file_assoc.htm

File Access takes me to command prompt rather than Explorer

Whenever I go to my command prompt and type a "c:\temp" or any other file/directory, it takes me to the command prompt rather than the file explorer. Can somebody please tell me how I can fix this so that all file/directory access from cmd takes me to the file explorer.
I have a Win2k3 64bit machine. Has this got anything to do with the 32bit/64bit explorer ?
To run Windows Explorer from command prompt you must type name of the executable:
explorer.exe
or just
explorer
To open a certain folder, pass the path as an arguments. Details here:
Command-Line Switches for Windows Explorer (MSDN KB)
See examples.
To open Explorer at the directory where your cmd is operating you can use
start .
and at a parent directory you can use
start ..

Resources