Created
July 18, 2024 20:20
-
-
Save Rapidhands/809800220872c4e6e0308d07f2db5b6e to your computer and use it in GitHub Desktop.
Update My Modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I'm using the PS module called PSWriteColor for a better and easy display in the console | |
if (-not (get-module -Name PSWriteColor -ListAvailable) ) | |
{ | |
#Module PSWriteColor not installed | |
Install-module -Name PSWriteColor | |
Import-Module PSWriteColor | |
} | |
function Update-MyModule | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([String])] | |
Param | |
( | |
# Description d’aide param1 | |
[Parameter(Mandatory = $true)] | |
[ValidateSet('CurrentUser', 'AllUsers')] | |
$Scope | |
) | |
Begin | |
{ | |
$InstalledModules = Get-InstalledPSResource -Scope $Scope | |
} | |
Process | |
{ | |
# initiating counter | |
$counter = 0 | |
foreach ($Item in $InstalledModules) | |
{ | |
$LastVersion = (Find-PSResource -Name $item.Name).Version | |
if ([Version]$LastVersion -gt [version]$Item.Version) | |
{ | |
Write-Verbose "$LastVersion is greater than $($Item.Version) for $($item.name)" | |
Find-PSResource -Name $($item.Name) | Update-PSResource -Scope $Scope -Force -SkipDependencyCheck | |
Write-Color "$($Item.Name) ", 'has been updated from ', "$($Item.Version) ", 'to ', "$Lastversion" -Color Yellow, Green, Yellow, Green, Yellow | |
$counter++ | |
Write-Verbose "removing old version for $($item.Name)" | |
Uninstall-PSResource -Name $($item.Name) -Version $($item.Version) -Scope $Scope -SkipDependencyCheck | |
} | |
} | |
} | |
End | |
{ | |
Write-Color "$counter ", 'modules has been updated for ', "$Scope" -Color Yellow, Green, Yellow | |
} | |
} | |
#Example of use | |
Update-MyModule -Scope CurrentUser | |
Update-MyModule -Scope AllUsers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment