4

I'm calling a PS script from Command Prompt using the following line

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

powershell "& 'script.ps1'" 

Is there a way to inject the relative path?

2 Answers 2

8
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...

param([string]$Directory) 

Then populating it from the command line invocation like...

Powershell C:\script.ps1 -Directory %cd% 
Sign up to request clarification or add additional context in comments.

Comments

0

To inject the relative path, where you start your batch file you have to write

powershell "& '%~dp0\script.ps1'" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.