Alex
Alex

Reputation: 44305

How to load a powershell module with a dynamic path?

I can load a module in powershell like that:

$ModuleName = $ScriptDir + "\functions.ps1"
. $ModuleName

But how to do this in one single line?

Upvotes: 1

Views: 1450

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

You're dot-sourcing a PowerShell script (extension .ps1), not importing a (script) module (extension .psm1).

You can do it in a single line like this:

. "$ScriptDir\functions.ps1"

or like this:

. (Join-Path $ScriptDir 'functions.ps1')

Upvotes: 1

Remco Eissing
Remco Eissing

Reputation: 1275

You could write something like this: Join-Path $ScriptDir "\functions.psm1" | Import-Module

So first safely combine the scriptdir and scriptname together and then use that for importing the module.

Upvotes: 0

Related Questions