Public/Install-pseModule.ps1
Function Install-pseModule { <# .SYNOPSIS Installs one or more specified PowerShell modules. .DESCRIPTION The Install-pseModule 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-pseModule -moduleName AzureAD Installs the latest version of the 'AzureAD' module. Requires administrative permissions. .EXAMPLE Install-pseModule -moduleName DHCPServer -version 1.0.5 Installs version 1.0.5 of the 'DHCPServer' module. .EXAMPLE "AzureAD", "ExchangeOnlineManagement" | Install-pseModule -currentUser -AcceptLicense Uses pipeline input to install 'AzureAD' and 'ExchangeOnlineManagement' modules for the current user, automatically accepting license agreements. .NOTES Author : owen.heaume Version: 1.0.0 - 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 { } } |