Ensure-Module is a helper function defined in the PowerShell profile that handles the full lifecycle of a module in a single call: it checks whether the module is installed, installs or updates it if needed, applies any module-specific configuration, and finally imports it into the current session.
Full function code
How it works
Check if the module is installed
Get-Module -ListAvailable searches all module paths on the system. If multiple versions exist, the highest version is selected.Find the latest version online
Find-Module queries the configured repository (PSGallery by default) for the current published version. -ErrorAction SilentlyContinue prevents failures when the gallery is unreachable.Install if not present
If
$installed is null the module has never been installed. Install-Module fetches it from the repository.Update if outdated
If a newer version exists online than what is installed,
Update-Module upgrades it in place.Apply VCF.PowerCLI configuration
When the module name is
VCF.PowerCLI, two additional settings are applied automatically: CEIP telemetry is disabled and invalid TLS certificates are set to be ignored (useful in lab and internal vSphere environments).Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
-Name | string | Yes | — | The exact module name as it appears in PSGallery. |
-Repository | string | No | PSGallery | The PowerShell repository to install from. |
-ForceInstall | switch | No | $false | Passes -Force to Install-Module and Update-Module, bypassing confirmation prompts and overwriting conflicts. |
Standalone usage
Ensure-Module can be called directly at any time, not just during profile load:
Automatic loading via $RequiredModules
The profile calls Ensure-Module for every entry in $RequiredModules at startup:
Module reference
The following modules are used across the PowerShell Toolkit. All are available on PSGallery unless otherwise noted.| Module | Purpose | Manual install command |
|---|---|---|
ActiveDirectory | AD user, group, and OU management | Included with RSAT; enable via Windows Features |
VCF.PowerCLI | VMware Cloud Foundation / vSphere automation | Install-Module -Name VCF.PowerCLI |
VMware.PowerCLI | VMware vSphere automation (legacy name) | Install-Module -Name VMware.PowerCLI |
Microsoft.Graph | Microsoft 365 and Entra ID via Graph API | Install-Module -Name Microsoft.Graph |
ExchangeOnlineManagement | Exchange Online mailbox and transport management | Install-Module -Name ExchangeOnlineManagement |
Microsoft.Online.SharePoint.PowerShell | SharePoint Online site and tenant management | Install-Module -Name Microsoft.Online.SharePoint.PowerShell |
MicrosoftTeams | Teams policies, channels, and user management | Install-Module -Name MicrosoftTeams |
AzureAD | Azure AD / Entra ID (deprecated) | Install-Module -Name AzureAD |
AzureAD is deprecated by Microsoft. Use Microsoft.Graph for new scripts. The Graph module covers all identity and directory operations that AzureAD provided, plus a much broader set of Microsoft 365 APIs.Install-Module defaults to -Scope AllUsers on Windows, which requires an elevated (administrator) session. Use -Scope CurrentUser to install without elevation — this is what Ensure-Module does by default. If a module must be available to all users on a shared machine, run the install command from an elevated session with -Scope AllUsers.