PowerShell: Introducing Import-PSModule

You may already know PsGet by Chaliy or PsGet by Anurse for PowerShell module distribution. They have created PowerShell module distribution mechanism with different approach.

Chaliy uses PowerShell script approach and customized module directory. Everytime you want to distribute your PowerShell module, you have to fork his github project to update Directory.xml content. Even tough the instruction in https://github.com/chaliy/psget/wiki/How-to-add-your-module-to-the-directory is very clear, however I think there is risk of incorrect Directory.xml modification.

Anurse uses PowerShell manifest approach and standard NuGet package to download and put module in appropriate location. In this case you can create a NuGet package containing PowerShell files and publish it to NuGet repository. Using his module, you will be able to get PowerShell module from NuGet repository right to your console. I like this approach because it uses standard NuGet package and publishing process. However, sometimes I want to be able to register the new module in my profile. I also want to ensure that I always use latest PowerShell Module package - ie. update local module automatically when there is new module in NuGet repository.

So then, I started to create Import-PSModule a PowerShell script module that will give you functionalities such as:

  1. Importing PS Module from NuGet repository
  2. Load imported module to current session
  3. Register imported module to current user profile or all user profile
  4. Un-register imported module from current user profile or all user profile
  5. Auto-update imported module when user start PowerShell session

To start using the module, start PowerShell session and type following

   [ps](new-object Net.WebClient).DownloadString("http://blog.libinuko.com/wp-content/plugins/download-monitor/download.php?id=1") | iex [/ps]

The script will perform following actions:

  1. Download latest ImportPSModule from central NuGet repository.
  2. Install the package to desired location. If you choose to install in User-Module or Global-Module, all future packages will be installed under Module path. If you choose to install in other folder, then all future package will be installed within installation folder.
  3. Load the ImportPSModule to the memory. It will also  download latest NuGet.CommandLine package if you don't have in your system.

So what do you waiting for? Start to install the module and publish your PowerShell module in NuGet repository. But don't forget TAG “PSModule” to your NuGet package.

If you want to validate InstallPSModule.ps1, please click here [download id="1”] .

Riwut Libinuko
Sr. Cloud Solution Architect

My research interests include distributed robotics, mobile computing and programmable matter.

comments powered by Disqus