1

I have a batch script to run powershell, how to set path to ps1 file if this file is in the same folder as executing BAT file? I use this but not working.

PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""./Reboot.ps1""' -Verb RunAs}";
Tutek
  • 11
  • 2
  • Check this - https://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file – stackoverflowusrone Feb 26 '20 at 10:00
  • I have changed file, still not works `PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""./Reboot.ps1""' -Verb RunAs}";` Reboot.bat and Reboot.ps1 are in the same folder. – Tutek Feb 26 '20 at 10:08
  • You don't need `-ExecutionPolicy Unrestricted` for the `-Command`, only for the `-File`. Also you're on Windows, so use the correct path separator, `.\Reboot.ps1` not `./Reboot.ps1`. – Compo Feb 26 '20 at 10:25

2 Answers2

1

Read call /? and Links relative to the Batch Script:

You can get the pathname of the batch script itself with %0, parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the batch script.

Use … -File ""%~dp0Reboot.ps1"" … (note that %~dp0 includes a trailing backslash!)

JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

With ./Reboot.ps you address the current working directory. What you need is the directory of the BAT file. Use %~d0%~p0/Reboot.ps instead.

An alternative could be to temporary set the working directory to the BAT's directory:

pushd %~d0%~p%"
rem run script with ./Reboot.ps1
popd

Obviously, if your script "Reboot.ps1" actually reboots the PC, you can skip the popd.

harper
  • 13,345
  • 8
  • 56
  • 105