Initialize-Module.ps1
19 Sep 2025Description
Purpose
Initialize-Module install and import modules from PowerShell Galllery.
Detailed Description
No detailed description provided.
Usage
No usage examples provided.
Notes
No additional notes.
Script
# Standalone application install script - (C)2024 Jonathan Pitre
#Requires -Version 5.1
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
$ProgressPreference = "SilentlyContinue"
$ErrorActionPreference = "SilentlyContinue"
# Set the script execution policy for this process
Try { Set-ExecutionPolicy -ExecutionPolicy 'ByPass' -Scope 'Process' -Force } Catch {}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Function Initialize-Module {
<#
.SYNOPSIS
Initialize-Module install and import modules from PowerShell Galllery.
.OUTPUTS
System.String
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true)]
[string]$Module
)
Write-Host -Object "Importing $Module module..." -ForegroundColor Green
# If module is imported say that and do nothing
If (Get-Module | Where-Object { $_.Name -eq $Module }) {
Write-Host -Object "Module $Module is already imported." -ForegroundColor Green
}
Else {
# If module is not imported, but available on disk then import
If ( [bool](Get-Module -ListAvailable | Where-Object { $_.Name -eq $Module }) )
{
$InstalledModuleVersion = (Get-InstalledModule -Name $Module).Version
$ModuleVersion = (Find-Module -Name $Module).Version
$ModulePath = (Get-InstalledModule -Name $Module).InstalledLocation
$ModulePath = (Get-Item -Path $ModulePath).Parent.FullName
If ([version]$ModuleVersion -gt [version]$InstalledModuleVersion) {
Update-Module -Name $Module -Force
Remove-Item -Path $ModulePath\$InstalledModuleVersion -Force -Recurse
Write-Host -Object "Module $Module was updated." -ForegroundColor Green
}
Import-Module -Name $Module -Force -Global -DisableNameChecking
Write-Host -Object "Module $Module was imported." -ForegroundColor Green
}
Else {
# Install Nuget
If (-not(Get-PackageProvider -ListAvailable -Name NuGet)) {
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Write-Host -Object "Package provider NuGet was installed." -ForegroundColor Green
}
# Add the Powershell Gallery as trusted repository
If ((Get-PSRepository -Name "PSGallery").InstallationPolicy -eq "Untrusted") {
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
Write-Host -Object "PowerShell Gallery is now a trusted repository." -ForegroundColor Green
}
# Update PowerShellGet
$InstalledPSGetVersion = (Get-PackageProvider -Name PowerShellGet).Version
$PSGetVersion = [version](Find-PackageProvider -Name PowerShellGet).Version
If ($PSGetVersion -gt $InstalledPSGetVersion) {
Install-PackageProvider -Name PowerShellGet -Force
Write-Host -Object "PowerShellGet Gallery was updated." -ForegroundColor Green
}
# If module is not imported, not available on disk, but is in online gallery then install and import
If (Find-Module -Name $Module | Where-Object { $_.Name -eq $Module }) {
# Install and import module
Install-Module -Name $Module -AllowClobber -Force -Scope AllUsers
Import-Module -Name $Module -Force -Global -DisableNameChecking
Write-Host -Object "Module $Module was installed and imported." -ForegroundColor Green
}
Else {
# If the module is not imported, not available and not in the online gallery then abort
Write-Host -Object "Module $Module was not imported, not available and not in an online gallery, exiting." -ForegroundColor Red
Exit 1
}
}
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.