Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading custom functions in PowerShell

Tags:

powershell

I've got some additional functions that I've defined in an additional PowerShell script file, that I'm trying to load in a main .ps1 file. However, when I call the .ps1 file from the PowerShell prompt it doesn't seem to run through those commands.

In the following code example, build_functions.ps1 has code that defines various custom functions. If I run the file separately (for example, running it by itself and then running through the primary script) it works fine. Build_builddefs.ps1 contains a number of variables that also need to be populated prior to running the primary script.

At the beginning of my primary script I have this:

.\build_functions.ps1 .\build_builddefs.ps1 

However, these don't seem to be run, because the primary script fails when it tries to execute the first custom function. What am I doing wrong?

like image 217
Sean Long Avatar asked Sep 12 '13 12:09

Sean Long


People also ask

How do you call a function in PowerShell?

A function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces. The function shown is a simple example that returns the version of PowerShell.


1 Answers

You have to dot source them:

. .\build_funtions.ps1 . .\build_builddefs.ps1 

Note the extra .

This heyscriptingguy article should be of help - How to Reuse Windows PowerShell Functions in Scripts

like image 66
manojlds Avatar answered Oct 17 '22 07:10

manojlds