# script may require Administrator privileges if you want to remove # module versions installed in "AllUsers" scope
# find ALL modules with more than one version and/or location: $multiversion = Get-Module-ListAvailable | Group-Object-Property Name | Sort-Object-Property Name | Where-Object Count -gt1
# ask user WHICH of these modules to clean? $clean = $multiversion | Select-Object-Property@{N='Versions';E={$_.Count}}, @{N='ModuleName';E={$_.Name}} | Out-GridView-Title'Select module(s) to clean'-PassThru
# get the todo list with the modules the user wants to clean: $todo = $multiversion | Where-Object Name -in$clean.ModuleName
$todo | ForEach-Object { $module = $_.Name # list all versions of a given module and let the user decide which versions # to keep and which to remove: $_.Group | Select-Object-Property Version, ModuleBase, ReleaseNotes | Sort-Object-Property Version | Out-GridView-Title"Module $module : Select all versions that you want to remove"-PassThru | Select-Object-ExpandProperty ModuleBase | # do a last confirmation dialog before permanently deleting the subversions: Out-GridView-Title'Do you really want to permanently delete these folders? CTRL+A and OK to confirm'-PassThru | Remove-Item-Recurse-Force
# folders where PowerShell looks for modules: $paths = $env:PSModulePath-split';' # finding actual module folders $modules = Get-ChildItem-Path$paths-Depth0-Directory | Sort-Object-Property Name
$modules | Select-Object-Property Name, @{N='Parent';E={$_.Parent.FullName}}, FullName | Out-GridView-Title'Select module(s) to permanently delete'-PassThru | Out-GridView-Title'Do you REALLY want to remove the modules below? CTRL+A and OK to confirm'-PassThru | Remove-Item-Path { $_.FullName } -Recurse-Force-WhatIf# remove -WhatIf to actually delete (as always at own risk)
最安全的方法是完全专注于通过 Install-Module 安装的模块,因为这样您永远不会意外删除 Windows 或属于其他软件产品的一部分的模块。
这行代码列出了由 Install-Module 安装的所有模块,并让您选择要(永久)删除的模块。
1 2 3 4
Get-InstalledModule | Out-GridView-Title'Select module(s) to permanently delete'-PassThru | Out-GridView-Title'Do you REALLY want to remove the modules below? CTRL+A and OK to confirm'-PassThru | Uninstall-Module