Public/Install-specModule.ps1
Function Install-specModule { <# .SYNOPSIS Installs one or more specified PowerShell modules. .DESCRIPTION The Install-specModule function installs PowerShell modules from the PowerShell Gallery (or other registered repositories). It performs the following actions: * Checks if a module is already installed. * Prompts for confirmation before installation (SupportsShouldProcess). * Installs the module using Install-Module. * Verifies installation success. .PARAMETER moduleName One or more PowerShell module names. Module names can be provided as: * A single string * An array of strings * Pipeline input .PARAMETER version If specified, attempts to install the given version number of the module. .PARAMETER currentUser Installs the module for the current user only. Does not require administrative permissions. .PARAMETER AcceptLicense Automatically accepts license agreements during installation. .EXAMPLE Install-specModule -moduleName AzureAD Installs the latest version of the 'AzureAD' module. Requires administrative permissions. .EXAMPLE Install-specModule -moduleName DHCPServer -version 1.0.5 Installs version 1.0.5 of the 'DHCPServer' module. .EXAMPLE "AzureAD", "ExchangeOnlineManagement" | Install-specModule -currentUser -AcceptLicense Uses pipeline input to install 'AzureAD' and 'ExchangeOnlineManagement' modules for the current user, automatically accepting license agreements. .NOTES * 1.0.0 - owen.heaume - Initial release #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] Param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$ModuleName, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [string]$Version, [switch]$CurrentUser, [switch]$AcceptLicense ) Begin { } Process { foreach ($module in $ModuleName) { try { Write-Host "Attempting to install module '$module'" -ForegroundColor DarkCyan if ($PSCmdlet.ShouldProcess("'$module'", "Install-Module")) { $installParams = @{ Name = $module Force = $true ErrorAction = 'Stop' } if ($currentUser.IsPresent) { $installParams['Scope'] = 'CurrentUser' } else { $installParams['Scope'] = 'AllUsers' } if ($AcceptLicense.IsPresent) { $installParams['AcceptLicense'] = $true } if ($version) { $installParams['RequiredVersion'] = $version } Write-Host "Installing module using parameters:" -ForegroundColor DarkGray $installParams Install-Module @installParams Write-Host "Module '$module' has been installed`n" -ForegroundColor DarkGreen } } catch { Write-Error "An unexpected error occurred: $_" } } } End { } } |