Home > Uncategorized > Package your powershell code

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
}

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: