Skip to content

Instantly share code, notes, and snippets.

@bender-the-greatest
Created January 3, 2017 03:55
Show Gist options
  • Save bender-the-greatest/57e60efbd92144df52f370ef801ad7db to your computer and use it in GitHub Desktop.
Save bender-the-greatest/57e60efbd92144df52f370ef801ad7db to your computer and use it in GitHub Desktop.
Installs modules if they are missing prior to import
# Installs modules if they are missing prior to importing
# Does not upgrade modules, only installs them if they are missing
#
# Example usage:
# install-and-import pscx
function install-and-import {
Param(
[Parameter(Mandatory=$true)]
[string]$moduleName
)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"
echo "Importing $moduleName"
if ( -not $( Get-Module -Name $moduleName -ListAvailable ) ) {
echo " Module $moduleName is not installed. Installing now..."
Install-Module -Force -AllowClobber $moduleName
}
Import-Module $moduleName
$ErrorActionPreference = $oldErrorActionPreference
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment