{"id":8718,"date":"2025-04-06T00:01:47","date_gmt":"2025-04-06T00:01:47","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=8718"},"modified":"2025-04-06T00:01:48","modified_gmt":"2025-04-06T00:01:48","slug":"remove-old-versions-of-psmodules","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/remove-old-versions-of-psmodules","title":{"rendered":"Remove Old versions of PSModules"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code class=\"\">#Requires -RunAsAdministrator\n\n# Get all installed modules with multiple versions\n$modules = Get-Module -ListAvailable | \n    Group-Object Name | \n    Where-Object { $_.Count -gt 1 }\n\n# Process each module with multiple versions\nforeach ($module in $modules) {\n    # Get all versions of the current module\n    $versions = $module.Group | Sort-Object Version -Descending\n    \n    # Keep the latest version (first one after sorting descending)\n    $latestVersion = $versions[0].Version\n    Write-Host \"Processing module: $($module.Name)\"\n    Write-Host \"Keeping latest version: $latestVersion\"\n    \n    # Remove all older versions\n    $versions | Where-Object { $_.Version -ne $latestVersion } | ForEach-Object {\n        try {\n            Write-Host \"Removing version: $($_.Version)\"\n            $modulePath = $_.ModuleBase\n            Remove-Item -Path $modulePath -Recurse -Force -ErrorAction Stop\n            Write-Host \"Successfully removed version: $($_.Version)\" -ForegroundColor Green\n        }\n        catch {\n            Write-Host \"Error removing $($module.Name) version $($_.Version): $_\" -ForegroundColor Red\n        }\n    }\n    Write-Host \"\" # Empty line for readability\n}\n\n# Verify the cleanup\nWrite-Host \"Cleanup complete. Verifying remaining modules...\" -ForegroundColor Yellow\nGet-Module -ListAvailable | \n    Group-Object Name | \n    Where-Object { $_.Count -gt 1 } | \n    ForEach-Object {\n        Write-Host \"Multiple versions still exist for $($_.Name)\" -ForegroundColor Red\n        $_.Group | ForEach-Object {\n            Write-Host \" - Version: $($_.Version) at $($_.ModuleBase)\"\n        }\n    }\n\nWrite-Host \"Script completed.\" -ForegroundColor Green<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8718","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=8718"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8718\/revisions"}],"predecessor-version":[{"id":8719,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8718\/revisions\/8719"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=8718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=8718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=8718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}