-1

I have powershell script which I am trying to execute using a batch file. I am not sure how to direct the .bat file to the powershell script without hardcoding the full path.

Both the script and .bat files are stored in the same folder. I've got this so far but it doesn't appear to do anything.

@ECHO OFF
REM PowerShell -WindowStyle Hidden -NoProfile....
SET ScriptDirectory = %~dpn0
SET ScriptPath = %ScriptDirectory% || "/" || counterscript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%ScriptPath%'";
Adam Khan
  • 53
  • 3
  • 6
  • Did you really intend to use "/" or instead "\"? If you `echo` out `ScriptPath` before you call `PowerShell`, what is the full value of `ScriptPath`? – gravity Jun 14 '19 at 15:12
  • I am completely new to batch scripting, so not really sure what I am doing. Put pieces together from the internet and understanding of other languages. – Adam Khan Jun 14 '19 at 15:14
  • There's a lot here that isn't clear - we really need to know what `ScriptPath` is prior to your PowerShell call (so echo out the variable), and also please note that you wouldn't use `-Command` at all to call a .ps1 script file, you'd use `-File` instead. – gravity Jun 14 '19 at 15:17
  • The recommended syntax for setting a variable is `Set "VariableName=VariableValue"`, you should therefore use e.g. `Set "ScriptDirectory=%~dp0"` Currently the way you have it, the spaces are also included, therefore your variable is named `ScriptDirectory `, so you'd need to use `%ScriptDirectory %` to access its content. Your other variable would need to be defined in the same manner, `Set "ScriptPath=%ScriptDirectory% || "/" || counterscript.ps1"`. – Compo Jun 14 '19 at 15:18
  • What I am looking for in a nutshell is to execute a powershell script called counterscript.ps1 with a .bat file which will also be placed in the same folder. I don't want to hard code the path. If possible, I would like the .bat script to locate where it is stored and then pick up a static file called counterscript.ps1 and run it. – Adam Khan Jun 14 '19 at 15:26
  • So long as that is achieved and I understand what is being done, I don't really need the above code. – Adam Khan Jun 14 '19 at 15:26
  • Have you tried, `PowerShell -NoProfile -ExecutionPolicy RemoteSigned -File "%~dp0counterscript.ps1"`, `PowerShell -NoProfile -ExecutionPolicy RemoteSigned -File ".\counterscript.ps1"` or `PowerShell -NoProfile -ExecutionPolicy RemoteSigned -File "counterscript.ps1"`? – Compo Jun 14 '19 at 20:09
  • I'll give this a go tomorrow and get back to you shortly. Thanks for the help. – Adam Khan Jun 16 '19 at 11:09
  • All three methods seem to do the trick. Which one would you recommend or is the best practice? – Adam Khan Jun 17 '19 at 10:22

1 Answers1

-1

I don't think you can. You can only hard code the path. Maybe there is some way that you can do it, but I don't know any other ways rather than hard coding. You could hard code the file name though.

BrickMan
  • 37
  • 8
  • Thanks BrickMan, I have tried this already but I am not look to hard code the file path as mentioned above. – Adam Khan Jun 14 '19 at 15:27
  • BrickMan, is it possible to not hard code the path into the batch file. Both the batch file and the script are placed in the same location. So I would prefer if the batch file can find the folder it's in dynamically and then look for the name of the script and execute it. As this will be more scabable as well. Thanks. – Adam Khan Jun 16 '19 at 11:08
  • Okay so I just edited the answer. I don't think there is any way. – BrickMan Jun 17 '19 at 06:23
  • Though you can hard code the file name – BrickMan Jul 16 '19 at 13:33