PowerShell Import-Module vs Dot Sourcing
Asked Answered
D

3

46

If I want to separate out some of my functionality from my main PowerShell script, I can either write that as a .ps1 file and dot source the same or I can create that as a .psm1 and import the same using Import-Module.

Which one is better and why?

Drona answered 14/2, 2013 at 19:15 Comment(3)
I find all the answers as informative and helpful. I can't mark any specific one as answer to this question. So, I am just giving an upvote for each of them. Hope it makes sense. Thanks for all your suggestions.Drona
It is still better if you choose an answer if you think you've got one, even combined. (btw, not mine, it just adds remarks).Cindiecindra
To zombie-echo @RomanKuzmin's comment, it's better to select an answer because then the question doesn't show up unanswered on the tag page. So if you've got a good set of answers, you could make comments of how folks could improve/what they missed and hope one becomes an uber-answer, but at some point (with the way SO is coded), it's best to accept something if you feel you've been answered.Dyanna
A
39

Modules are best for libraries. They give you more control over what is exported from the module. That is by default all script variables in a PSM1 file are private - not visible outside the module when it's imported. Likewise, all functions are public. However, you can use Export-ModuleMember in your PSM1 file to control exactly what variables, functions, aliases, cmdlets, etc that you export from your module. Modules can also be removed from your session which is a major difference with dotsourcing a .PS1 script. Another difference is that module functions are namespaced by the module they're in so you can easily access identically named module-based functions by prefixing the module name and a "\" to the function name e.g. PSCX\Get-Uptime. In ISE, this prefix also invokes intellisense support.

I generally recommend going with modules. :-)

Allyl answered 14/2, 2013 at 20:1 Comment(2)
What are your thoughts on loading (dotsourcing) .PS1 scripts from within a module? This is Pester's approach.Kingdon
I think that is fine. The dot sourced scripts would be scoped to that module.Allyl
M
23

Dotsourcing + script and modules are 2 different things. Modules are great to collect/group functions and cmdlets that you would use in a script. If you have functions that you want to use interactively(you call a function in a console), then module can be a great fit.

If you have one big script that you run to .. let's say "migrate a file share", or a single script that you call regulary using Task Scheduler, then dot-sourcing easier.

It depends on what you need. Summary:

  • If you just use the the functions/scripts in THIS situation(script/job) = use dot source.
  • If you use common functions/script that are used in other scripts too OR you want to call some of the functions interactively etc. = use module.
Merilyn answered 14/2, 2013 at 19:22 Comment(0)
C
8

A few module features in addition to other answers.

  • In PowerShell V3 one does not have to call Import-Module in order to use module's exported commands. This is especially useful when commands are used interactively. PowerShell somehow caches and knows all available module commands and even their help Get-Help SomeCommand (this is not true for module help Get-Help about_SomeModule, though).

  • There are several subtle differences in behavior of dot-sourced functions and script module functions. It is not easy to list them all, here is just one example: Strange behavior with Powershell scriptblock variable scope and modules, any suggestions? Sometimes using script modules gets painful, especially when one discovers unwanted differences and issues too late, i.e. simple things work fine in the beginning of development but complex things start to go wrong later.

All in all, normally I use modules except cases when they do not work well. One of them is invoking user script blocks passed in script module functions.

Cindiecindra answered 14/2, 2013 at 23:58 Comment(3)
How can Powershell know about a text file I just created and called it someModule.psm1?! Does Powershell permanently scan my file system for such files?!Realty
On certain occasions, PowerShell scans the standard module locations and reads and caches new and changed modules. The details are not documented, as far as I know.Cindiecindra
As far as I know, Powershell only checks files inside the folder specified by $Env:PSModulePath. (see here)Realty

© 2022 - 2024 — McMap. All rights reserved.