1

I have a header.ps1 file that need to be called from the Main.ps1 script. They are both under the same directory.

In the header.ps1 file, it started off with following...

[CmdletBinding()]
param(
[String]$PROJECTNAME =  'PSToolBox',
[string]$SCRIPTNAME = 'accountSelector.ps1'
)

In the Main.ps1 file, the following does work except I cannot use header.ps1 parameters.

This works:

. ($PSScriptRoot + '\header.ps1')

This does NOT work:

. (($PSScriptRoot + '\header.ps1') -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1')

So, how to dot the relative path with passing the parameters in powershell script?

Liang Cui
  • 131
  • 1
  • 14
  • 1
    More streamlined, using string interpolation: `. "$PSScriptRoot\header.ps1" -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1'` – zett42 Jun 28 '23 at 19:55

1 Answers1

4

You need to pass the command name/path separately from the parameter arguments (this applies not only to ., the same goes for invocations with &):

. ($PSScriptRoot + '\header.ps1') -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1'
# \_____________________________/ \_______________________________________________/
#       first the command                     ... and then the arguments
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206