How to vendor a PowerShell script

Published on Tuesday, 23 February 2016

What is vendoring?

Vendoring is where you create a standalone script that contains a copy of all of the modules it uses. This is useful for situations where you don’t know the state of the target machine and want to make sure the script runs with specific versions of modules. This is typically useful for a 3rd party service provider when providing scripts for a customer to run.

When not to use vendoring?

Whenever possible I would recommend avoiding vendoring. The reason being is if you have a large number of scripts on a system using this technique and you update a module that you use, you have to go around to every single script and update it individually.

How to vendor:

  1. The first step is to create a folder to host your script and other files in.
  2. Inside the folder create a modules directory. Copy all of the modules you want to use into this directory as you would the modules directory in your profile.
  3. At the top of your script put the following
$env:psmodule = "$PSScriptRoot\Modules"
  1. Now you can use Import-Module like normal and it will pull modules from your local folder instead of the system modules directories.

Note: You will need to copy all the modules you wish to use into the modules folder created above if you want to use them or pass their full path when calling import-module.