Skip to content

Instantly share code, notes, and snippets.

@dancing-groot
Last active November 24, 2025 18:53
Show Gist options
  • Select an option

  • Save dancing-groot/b0521f8152b2aabc6876db7ea1cc922e to your computer and use it in GitHub Desktop.

Select an option

Save dancing-groot/b0521f8152b2aabc6876db7ea1cc922e to your computer and use it in GitHub Desktop.
Update-RequireModules
function Update-RequireModules
{
<#
.SYNOPSIS
Checks repository pre-requisites and install/update the modules needed for the script using PSResouceGet
.LINK
https://gist.github.com/dancing-groot/b0521f8152b2aabc6876db7ea1cc922e
.NOTES
Version: 2025.11.24
Author: @dancing-groot
#>
[cmdletBinding()]
param
(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelinebyPropertyName)]
[string[]]$Module
)
begin
{
if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
if ($PSBoundParameters['Verbose']) { $VerbosePreference = 'Continue' }
# Set PSGallery Repository to v2 if needed
try
{
Get-PSResourceRepository -Name PSGallery | Out-Null
}
catch
{
Write-Verbose "Setting v2 attribute for the repository 'PSGallery'"
Set-PSResourceRepository -Name PSGallery -ApiVersion v2
}
# Trust PowerShell Gallery
if (Get-PSResourceRepository | Where-Object { $_.Name -eq "PSGallery" -and $_.Trusted -eq $false })
{
Write-Verbose "Trusting the Repository 'PSGallery'"
Set-PSResourceRepository -Name "PSGallery" -Trusted
}
}
process
{
if (-not (Get-PSResource -Name $Module -ErrorAction SilentlyContinue))
{
Install-PSResource -Name $Module -Scope CurrentUser
Write-Verbose "Microsoft Graph Module '$Module' Installed"
}
else
{
Update-PSResource $Module -Scope CurrentUser -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
} # Update-RequireModules
"Az.Accounts", "Az.KeyVault" | Update-RequireModules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment