Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a powershell script from another script with $PSScriptRoot?

I'm wondering what I'm missing here. I have a powershell script that calls another script with some parameters to execute as a way to keep things tidy. Here is what works:

C:\Scripts\Project\coolscript.ps1 -projname 'my.project' -domain 'work'

I want others to be able to use this script without having to change anything, so I thought I could make the path relative instead of the full one starting from C: so I thought I could execute the script like this:

$pathname = $PSScriptRoot + '\coolscript.ps1' 
$pathname -projname 'my.project' -domain 'work'

however I get an error that says 'unexpected token in expression or statement for everything after $pathname

ANy ideas what I'm missing? Thank you

like image 601
datadawg2000 Avatar asked Sep 06 '25 06:09

datadawg2000


1 Answers

Use the Call operator (&) as follows:

& $pathname -projname 'my.project' -domain 'work'

Call operator &

Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings or script blocks. The call operator executes in a child scope. For more about scopes, see about_scopes.

like image 166
JosefZ Avatar answered Sep 07 '25 19:09

JosefZ