PowerShell / PowerShellGetv2

PowerShellGet is the Package Manager for PowerShell
https://www.PowerShellGallery.com
MIT License
429 stars 138 forks source link

Uninstall-Module doesn't remove/delete module #610

Open johnwc opened 4 years ago

johnwc commented 4 years ago

I am trying to remove older module versions that are no longer being used. I run the command to point to a specific version of the install module, but nothing happens. It looks from the vernose output as if it is using an incorrect path for the module removal.

Steps to reproduce

PS C:\Users\user> Get-Module AWS.Tools.APIGateway -ListAvailable

    Directory: C:\Users\user\OneDrive\Documents\PowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Binary     4.0.6.0               AWS.Tools.APIGateway                Core,Desk {Add-AGResourceTag, Clear-AGStageAuthorizersCache, Clear-AGStageCache, Get-AGAccount…}
Binary     4.0.5.0               AWS.Tools.APIGateway                Core,Desk {Add-AGResourceTag, Clear-AGStageAuthorizersCache, Clear-AGStageCache, Get-AGAccount…}

PS C:\Users\user> Uninstall-Module -Name AWS.Tools.APIGateway -RequiredVersion 4.0.5.0 -Force -Verbose
VERBOSE: Performing the operation "Uninstall-Module" on target "Version '4.0.5.0' of module 'AWS.Tools.APIGateway'".
VERBOSE: Successfully uninstalled the module 'AWS.Tools.APIGateway' from module base 'C:\Users\user\Documents\PowerShell\Modules\AWS.Tools.APIGateway\4.0.5.0'.
PS C:\Users\user> Get-Module AWS.Tools.APIGateway -ListAvailable

    Directory: C:\Users\user\OneDrive\Documents\PowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Binary     4.0.6.0               AWS.Tools.APIGateway                Core,Desk {Add-AGResourceTag, Clear-AGStageAuthorizersCache, Clear-AGStageCache, Get-AGAccount…}
Binary     4.0.5.0               AWS.Tools.APIGateway                Core,Desk {Add-AGResourceTag, Clear-AGStageAuthorizersCache, Clear-AGStageCache, Get-AGAccount…}

Expected behavior

Correctly find the installed location and remove module.

Actual behavior

Doesn't use correct path, so doesn't remove module.

Environment data

PS C:\Users\user> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.2
PSEdition                      Core
GitCommitId                    7.0.2
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
rjmholt commented 4 years ago

This should be transferred to https://github.com/PowerShell/PowerShellGet

GitHub
PowerShell/PowerShellGet
PowerShellGet is the Package Manager for PowerShell - PowerShell/PowerShellGet
daxian-dbw commented 4 years ago

/cc @alerickson @SteveL-MSFT Can you please transfer this issue to the PowerShellGet repo?

alerickson commented 4 years ago

@daxian-dbw @SteveL-MSFT I can't transfer the issue, but it should go in PowerShell/PowerShellGetv2

daxian-dbw commented 4 years ago

@alerickson Can you grant me the write permission to PowerShellGetv2 and PowerShellGet repos? So in future I can transfer the related issues to those repos.

alerickson commented 4 years ago

@daxian-dbw just added you to both repos

rbeesley commented 3 years ago

I don't know that this is related, but I'm hitting an issue where PowerShellGet isn't uninstalling modules although it presents slightly different behavior:

PS > Get-InstalledModule -Name oh-my-posh | uninstall-module -Force -Verbose -Debug
DEBUG: 00:00:00.0000002 Calling New() : MethodName = 'GetDynamicOptions'
DEBUG: 00:00:00.0000262 Debug: True
DEBUG: 00:00:00.0000391 Name: oh-my-posh
DEBUG: 00:00:00.0000502 Verbose: True
DEBUG: 00:00:00.0000610 Force: True
DEBUG: 00:00:00.0000715 RequiredVersion: 2.0.449
DEBUG: 00:00:00.0005259 INVOKING PowerShell Fn Get-DynamicOptions with args Provider that has length 1
DEBUG: 00:00:00.0009809 In PowerShellGet Provider - 'Get-DynamicOptions'.
DEBUG: 00:00:00.0016924 Done calling powershell «Get-DynamicOptions» «PSModule»
DEBUG: 00:00:00.0072179 Calling New() : MethodName = 'GetDynamicOptions'
DEBUG: 00:00:00.0072379 Debug: True
DEBUG: 00:00:00.0072500 Name: oh-my-posh
DEBUG: 00:00:00.0072605 Verbose: True
DEBUG: 00:00:00.0072714 Force: True
DEBUG: 00:00:00.0072820 RequiredVersion: 2.0.449
DEBUG: 00:00:00.0078836 INVOKING PowerShell Fn Get-DynamicOptions with args Install that has length 1
DEBUG: 00:00:00.0083048 In PowerShellGet Provider - 'Get-DynamicOptions'.
DEBUG: 00:00:00.0099496 Done calling powershell «Get-DynamicOptions» «PSModule»
DEBUG: 00:00:00.0204021 Calling New() : MethodName = 'GetInstalledPackages'
DEBUG: 00:00:00.0214604 Type: Module
DEBUG: 00:00:00.0220499 Force: True
DEBUG: 00:00:00.0225988 Name: oh-my-posh
DEBUG: 00:00:00.0232032 MessageResolver: Microsoft.PowerShell.PackageManagement.Cmdlets.GetMessageString
DEBUG: 00:00:00.0244850 ProviderName: PowerShellGet
DEBUG: 00:00:00.0252558 RequiredVersion: 2.0.449
DEBUG: 00:00:00.0258002 Verbose: True
DEBUG: 00:00:00.0264006 Debug: True
DEBUG: 00:00:00.0271519 INVOKING PowerShell Fn Get-InstalledPackage with args oh-my-posh, 2.0.449, ,  that has length 4
DEBUG: 00:00:00.0278683 In PowerShellGet Provider - 'Get-InstalledPackage'.
DEBUG: 00:00:00.0285351 OPTION: Type => Module
DEBUG: 00:00:00.0292151 OPTION: Force => True
DEBUG: 00:00:00.0304191 OPTION: Name => oh-my-posh
DEBUG: 00:00:00.0309347 OPTION: MessageResolver => Microsoft.PowerShell.PackageManagement.Cmdlets.GetMessageString
DEBUG: 00:00:00.0314397 OPTION: ProviderName => PowerShellGet
DEBUG: 00:00:00.0319327 OPTION: RequiredVersion => 2.0.449
DEBUG: 00:00:00.0324219 OPTION: Verbose => True
DEBUG: 00:00:00.0329184 OPTION: Debug => True
DEBUG: 00:00:00.1316092 Done calling powershell «Get-InstalledPackage» «PSModule»
DEBUG: 00:00:00.1347450 Uninstalling package oh-my-posh with provider PowerShellGet

Confirm
Are you sure you want to perform this action?
Performing the operation "Uninstall-Module" on target "Version '2.0.449' of module 'oh-my-posh'".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
DEBUG: 00:00:13.3584431 Calling New() : MethodName = 'UninstallPackage'
DEBUG: 00:00:13.3615926 Type: Module
DEBUG: 00:00:13.3629947 Force: True
DEBUG: 00:00:13.3641643 Name: oh-my-posh
DEBUG: 00:00:13.3648768 MessageResolver: Microsoft.PowerShell.PackageManagement.Cmdlets.GetMessageString
DEBUG: 00:00:13.3655973 ProviderName: PowerShellGet
DEBUG: 00:00:13.3663134 RequiredVersion: 2.0.449
DEBUG: 00:00:13.3670592 Verbose: True
DEBUG: 00:00:13.3677144 Debug: True
DEBUG: 00:00:13.3687986 INVOKING PowerShell Fn Uninstall-Package with args NuGet|oh-my-posh|2.0.449|https://www.powershellgallery.com/api/v2|Module that has length 1
DEBUG: 00:00:13.3693578 In PowerShellGet Provider - 'Uninstall-Package'.
DEBUG: 00:00:13.3706060 The FastPackageReference is 'NuGet|oh-my-posh|2.0.449|https://www.powershellgallery.com/api/v2|Module'.
DEBUG: 00:00:13.3711738 OPTION: Type => Module
DEBUG: 00:00:13.3716925 OPTION: Force => True
DEBUG: 00:00:13.3724713 OPTION: Name => oh-my-posh
DEBUG: 00:00:13.3745132 OPTION: MessageResolver => Microsoft.PowerShell.PackageManagement.Cmdlets.GetMessageString
DEBUG: 00:00:13.3757832 OPTION: ProviderName => PowerShellGet
DEBUG: 00:00:13.3767337 OPTION: RequiredVersion => 2.0.449
DEBUG: 00:00:13.3774324 OPTION: Verbose => True
DEBUG: 00:00:13.3787455 OPTION: Debug => True
VERBOSE: Successfully uninstalled the module 'oh-my-posh' from module base 'C:\Users\[REDACTED]\Documents\PowerShell\Modules\oh-my-posh\2.0.449'.
Uninstall-Package: C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:12710
 Line |
12710 |  …        $null = PackageManagement\Uninstall-Package @PSBoundParameters
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      | Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

PS >

You can see in the Verbose message that it thinks it successfully uninstalled the module, but the module is still there. The steps to reproduce show a similar message but without the error.

ghost commented 3 years ago

This seems to be quite a prolific issue - I'm also having this issue, and based on the number of issues, so are other people. Has there been any developments on resolving this issue?

ckolumbus commented 3 years ago

I had the same issue, and the solution was completely unrelated to PowershellGet ... we have OneDrive configured and my profile resides under C:\UserData\<user>\OneDrive\Documents\PowerShell\Modules.

I CANNOT DELETE anything from there (neither via Explorere and obviously also not via Uninstall-Module). Seems to have something to do with how OneDrive treats files there. I CAN MOVE it away, also to the RecycleBin.

So the solution for me: remove it manually, and sometimes figure out how change this sub-optimal configuration to automatically place all modules in my OneDrive folder.

tested with pwsh core 7.1.3 on Win10-20H2

Update: PowerShell 5.1 provides an error message that hints in that direction:

PackageManagement\Uninstall-Package : Access to the cloud file is denied
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:12733 char:21
+ ...        $null = PackageManagement\Uninstall-Package @PSBoundParameters
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-Package],
   Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand,Mic
   rosoft.PowerShell.PackageManagement.Cmdlets.UninstallPackage
SydneyhSmith commented 3 years ago

@timrossback were you also seeing this issue with a OneDrive path?

ghost commented 3 years ago

@SydneyhSmith Yeah, I was.

rbeesley commented 3 years ago

OneDrive seems like a common theme. My Modules would have been under OneDrive too.

Paurian commented 3 years ago

I agree with OneDrive possibly being the defining element in this bug. Even after manually removing the directories associated with the module, PowerShell still thought it was there.

PS C:\Users\paurian> Uninstall-Module -Name MicrosoftPowerBIMgmt -Force -AllVersions -Verbose
VERBOSE: Performing the operation "Uninstall-Module" on target "Version '1.0.867' of module 'MicrosoftPowerBIMgmt'".
VERBOSE: Successfully uninstalled the module 'MicrosoftPowerBIMgmt' from module base 'C:\Users\paurian\OneDrive\Home Drive\My Documents\PowerShell\Modules\MicrosoftPowerBIMgmt\1.0.867'.

...

PS C:\Users\paurian> Get-InstalledModule -Name MicrosofTPowerBIMgmt

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
1.0.867              MicrosoftPowerBIMgmt                PSGallery            Microsoft Power BI PowerShell - All cmdlets for Microsoft Power BI
johnwc commented 3 years ago

I don't believe OneDrive itself is the culprit, only the reason. I believe the culprit is the My Documents folder has folder redirection applied to it.

rbeesley commented 3 years ago

@johnwc, likely. Leveraging the backup of OneDrive does this, is the encouraged setting, and is probably the most common way this situation presents itself, therefore it is why it is why those of us who have seen this issue have it in common. However, I agree that it is probably not the "root cause." It should be pretty easy to replicate the issue if OneDrive isn't enabled and My Documents is remapped, such as one might do if they have another disk volume. I expect that will fail too.

florinmicle commented 2 years ago

Any ideas/workarounds? I just ran into this issue as well...

anthony-kantola commented 11 months ago

I'm still seeing this issue as well. The user scoped PowerShell module directory is within OneDrive and I can't remove modules from there. Is there a workaround?

rbeesley commented 11 months ago

@anthony-kantola it's been awhile since this blocked me. I think I probably closed all my PowerShell instances and then opened a PowerShell instance with -noprofile, used cmd, or modified the directory in Explorer, to manually remove the module. While less ideal than using the shell as intended, something worked for me. The big thing is just making sure that those files aren't being used when they are purged. It was a nuisance, but I found some combination that worked, just not as intended.