How do I set an executable's working directory via the command line, prior to executing it?
To explicitly set the working directory, a PowerShell solution would be to use the Start-Process
cmdlet with the -WorkingDirectory
parameter.
Start-Process -FilePath notepad.exe -WorkingDirectory c:\temp
Using the alias start
, positional parameter, and partial parameter name this could be written as:
start notepad.exe -wo c:\temp
CMD also has a START
command. For this, use the /D
parameter to specify the working directory:
START /D c:\temp notepad.exe
The below will work, make appropriate substitutions and save it with a .cmd
extension.
@echo off
C:
chdir C:\desired\directory
C:\full\path\of\command.exe
Put this batch file in a directory in your %PATH%
and you should be able to invoke it from any cmd.exe
instance.