Question:
I’m calling a PS script from Command Prompt using the following line
1 2 |
powershell "& 'c:/script.ps1'" |
However I need the script to be relative to the command prompt window. So if command prompt was looking at C:
then the script would effectively be
1 2 |
powershell "& 'script.ps1'" |
Is there a way to inject the relative path?
Answer:
1 2 |
powershell "& '%cd%\script.ps1'" |
or if you mean to use the directory from which the batch file was launched in the powershell script then you would use a parameter by putting this type of line at the start of your PS script…
1 2 |
param([string]$Directory) |
Then populating it from the command line invocation like…
1 2 |
Powershell C:\script.ps1 -Directory %cd% |