Search code examples
powershellparametersrelative-pathdot-source

how to dot the relative path with arguments in powershell script


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?


Solution

  • 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