Archive for January, 2017

Package your powershell code

Powershell files

Probably the most natural way to store a couple of lines of powershell script code is to create a file with the extension .ps1. A .ps1 file can be thought of in two ways

  • A script containing utility functions and variables. Such a file can be loaded into your current session using dot sourcing.
  • A function named as the script file and with a function body defined within the script. Parameters must be defined using a param(…) block.

Powershell script modules

Sometimes there is a need to deliver powershell code to a third party; maybe another developer team, the powershell community,  a company or such. Maybe there is a group of related functions and script files that fits naturally together. A powershell module might be the right thing in this case. It is a bit more heavy weight than just a simple script file but, in the simplest form it is still simple – basically you can rename your .ps1 file to .psm1 and you have a powershell module.

I mentioned that there might be several script files that should be grouped together; how is that done?  The most commonly used technique seems to be to simply dotsource the script files into the module file. Some people use snippets like below to batch import a selection of files.

$scripts = Get-ChildItem $PSScriptRoot\scripts -Filter "*.ps1"

foreach ($script in $scripts){
. $script.FullName


Categories: Uncategorized