Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Powershell from Command Prompt Relative Path

Tags:

powershell

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?

like image 274
heymega Avatar asked Feb 10 '23 23:02

heymega


1 Answers

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%
like image 97
Trey Nuckolls Avatar answered Feb 13 '23 12:02

Trey Nuckolls