0

I am doing a powershell task manually i.e. opening the powershell as an administrator and executing the command pushd D:\PowerShellTry and command .\FileWatcher.ps1 in powershell.

I wrote a myScript.bat to automate the manual task through a bat file. Below is the code which I wrote for my bat file to automate it:

powershell -Command "& {Start-Process powershell.exe -Verb RunAs; pushd D:\PowerShellTry; .\FileWatcher.ps1}"

But it is not working. How can I do this correctly?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

4
powershell -Command "Start-Process -Verb RunAs powershell.exe '-NoExit pushd D:\PowerShellTry; .\FileWatcher.ps1'"

Note the use of embedded '...' quoting and the -NoExit switch to keep the elevated session open, so you can examine the script's output.

As for what you tried:

  • Note that here's no reason to use "& { ... }" in order to invoke code passed to PowerShell's CLI via the -Command (-c) parameter - just use "..." directly, as shown above.
    (Older versions of the CLI documentation erroneously suggested that & { ... } is required, but this has since been corrected.)

  • By placing ; after Start-Process powershell.exe -Verb RunAs, you terminated the command right there, launching an interactive elevated PowerShell session asynchronously; the subsequent pushd ...; .\... commands then executed in the original, non-elevated session.

    • Instead, the commands to be run with elevation must be passed as arguments to the powershell.exe instance launched with -Verb Runas, as shown above, just like with the outer powershell.exe call (the -Command CLI parameter is implied, as is Start-Process' -ArgumentList parameter).
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I tried to use the above command provided by you. It is not working as per my need. Powershell window is opening but it close automatically. I cannot see if it has executed the 2 certain commands. I also do not want to close the powershell window. Thank you. – Kumar Anil Chaurasiya May 18 '23 at 06:42
  • 1
    @KumarAnilChaurasiya, you need the `-NoExit` switch to keep the session open. (which is a requirement you didn't mention in your question). Please see my update. – mklement0 May 18 '23 at 06:51
  • Thank you. I found another way which does my job -: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell -Verb RunAs -ArgumentList '-NoExit', '-Command', 'pushd D:\PowerShellTry";.\FileWatcher.ps1' " – Kumar Anil Chaurasiya May 18 '23 at 07:39
  • Your suggestion also works. thanks. – Kumar Anil Chaurasiya May 18 '23 at 07:39
  • Glad to hear it, @KumarAnilChaurasiya. There's a stray `"` in your command, after `PowerShellTry`. I see that you've added additional parameters and spelled out `-ArgumentList`, which is _positionally implied_ in my command, for brevity. Also, you've passed _multiple_ arguments to `-ArgumentList`, and while that may be _conceptually_ desirable, a long-standing bug unfortunately makes it better to use a _single_ string that encodes _all_ arguments instead: see [this answer](https://stackoverflow.com/a/71271756/45375). – mklement0 May 19 '23 at 15:46