Home > Software design >  What is the difference between opening application by cmd/ps or by click?
What is the difference between opening application by cmd/ps or by click?

Time:04-12

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

CodePudding user response:

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