0

I have a Powershell script that takes two arguments.

param([string]$folder, [string]$filename)

When I open a Powershell shell, navigate to the folder that contains the script, and run it from there, the script is executed correctly:

C:\scriptfolder> ".\script.ps1" "C:\folder for argument" "filename.ext"

When I try to run the script from a different folder, I get an error message:

C:\otherfolder> "C:\scriptfolder\script.ps1" "C:\folder for argument" "filename.ext"

At line:1 char:nn

Unexpected token '"C:\folder for argument"' in expression or statement.

How can I run this script from another folder?

Hobbes
  • 1,964
  • 3
  • 18
  • 35
  • You need to dot-source the path to the ps1 file `. "C:\scriptfolder\script.ps1" "C:\folder for argument" "filename.ext"` or use the `&` call operator – Theo Feb 03 '23 at 12:57
  • That works, so I'd accept that as an answer. – Hobbes Feb 06 '23 at 08:52

1 Answers1

1

In order not to leave this question as 'Unanswered', here my comment as answer

There are two ways to do what you want. The first is by using dot-sourcing:

. "C:\scriptfolder\script.ps1" "C:\folder for argument" "filename.ext"

The second method would be by using the `& call operator

& "C:\scriptfolder\script.ps1" "C:\folder for argument" "filename.ext"

The difference between the two methods is that a script that is invoked using dot-sourcing runs in the current scope, while with the second method, using the call operator, scripts and functions run in a child scope.

You can find an excellent explanation in the answer here

Theo
  • 57,719
  • 8
  • 24
  • 41