PowerShell: dot source scripts into your module

You can create wrapper modules around existing library script files you “dot source” ‘d into your PowerShell 1.0 code by dot sourcing them into your module.

Problem is that from a module your current location is the location of the script that first imported your module.

You can get the location of your module using the PowerShell variable $PSScriptRoot.

So in your module file Xml.mps1 you can do something like:

Write-Host "Module Location: $PSScriptRoot"
. $PSScriptRoot\..\..\..\DotNet2\MastDeploy\MastPowerShellLib\MastDeploy-Logging.ps1
. $PSScriptRoot\..\..\..\DotNet2\MastDeploy\MastPowerShellLib\MastDeploy-DebugTooling.ps1
. $PSScriptRoot\..\..\..\DotNet2\MastDeploy\MastPowerShellLib\MastDeploy-Util-Xml.ps1

Export-ModuleMember -function Xml*

Using this approach you can keep using the “dot source” approach in your PowerShell 1.0 code until you have time to refactor it, and start using modules in your PowerShell 2.0 code which prevents reloading code when the code is already available.

No Comments