Run a PowerShell script from another

What is the best and right way to run a PowerShell script from another?

I have a script a.ps1 from which I want to call b.ps1, which performs another task.

Let me know your suggestions. Is it better to use point search here?

+6
powershell
source share
2 answers

Dot sourcing will run the second script as if it were part of the caller - all changes to the scope of the script will affect the caller. If this is what you need, then dot-source,

However, it is more common to call another script as if it were a function (a script can use param and functional level attributes in the same way as a function). In many ways, a script is a PowerShell function, with a file name that replaces the function naming.

+5
source share

Dot sourcing makes it easy at a later stage to convert your script (s) into a module, you do not have to change script (s) to functions.

Another advantage of a point source is that you can add this function to your shell by adding a file containing functions in Microsoft.PowerShell_profile.ps1, which means you always have access to them (eliminating the need to worry about paths, etc.). d.).

I have a short host entry at the top of my point source files with the name of the function and general parameters, and I dot is the source of the functions in my profile. Each time I open PowerShell, the list of functions in my profile is scrolled (If, like me, you often forget the exact names of your functions / files. You will evaluate this over time, as the number of functions starts to accumulate).

0
source share

All Articles