Search code examples
powershellwindows-10working-directoryxtermjsnode-pty

How do I set the start path of powershell?


As far i know(let me know if you have a better solution) to start powershell in a directory you have to do something like this:

powershell.exe -NoExit -command "& {Set-Location C:\my\path\here}"

Now I'm trying to reproduce this with spawn from node-pty like this:

  const shell = isWindows ? 'powershell.exe' : 'bash';
  const args = !isNullOrEmptyOrWhiteSpaced(shellStartFolder) ?
                ["-command",  `"& {Set-Location ${shellStartFolder}}"`] : [];
  return spawn(shell, args, {
    name: 'xterm-color',
    cols: DefaultTerminalSize.cols,
    rows: DefaultTerminalSize.rows,
    cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
    env: process.env as INonUndefinedEnv
  });

but pseudo terminal (i'm using xterm.js) end up like this (give shellStartFolder is "C:\\")

image

What am I missing?


Solution

  • As mentioned in the comments, the spawn() function already takes an argument that specifies the initial working directory: cwd.

      const shell = isWindows ? 'powershell.exe' : 'bash';
      const args = [];
    
      return spawn(shell, args, {
        name: 'xterm-color',
        cols: DefaultTerminalSize.cols,
        rows: DefaultTerminalSize.rows,
        cwd: isWindows ? (!isNullOrEmptyOrWhiteSpaced(shellStartFolder) ? shellStartFolder : process.env.USERPROFILE) : process.env.HOME,
        env: process.env as INonUndefinedEnv
      });