One Powershell Script calling another with parameters

EDIT: dot-sourcing the script as shown in my original answer will load the script and variables, objects, functions, etc. into the current session – this might have unintended consequences in certain circumstances.

The alternative is to use the & call operator which runs the script in its own scope:

& "C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1" -ServerName medsys-dev

The problem is you have a space in your file path so you need to wrap the entire path in single quotes so it’s recognised correctly. (See about_Quoting_Rules for more info on single vs double quotes). It ends up being a messy command in the end:

Invoke-Expression "&'C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1' -ServerName medsys-dev"

dot-sourcing is much nicer as you just wrap the script path in double quotes and leave the params as-is:

."C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1" -ServerName medsys-dev

Leave a Comment