Skip to content

Uinstall-PSResource on OneDrive Error: Access to the path is denied #1793

Description

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues.

Steps to reproduce

Hi All,

I am using Windows 11 (2024H2 26100.3194). My Document Folder is Redirected to Onedrive.
I am using Onedrive Build 25.015.0126.0002 (64-bit)
I am using PowerShell 7.5.0 and Microsoft.PowerShell.PSResourceGet 1.1.0

I regularly have Errors when uninstalling PowerShell Modules

Install the Module works fine
Install-PSResource -Name ExchangeOnlineManagement -Version 3.7.0 -Scope CurrentUser

Uninstalling the Module throws: Acces to the path is denied
Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck
Uninstall-PSResource: Parent directory 'C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0' could not be deleted: Access to the path '\?\C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\en-US' is denied

Result:
All the Files are deleted, but the Folder Structure is still there

Get-ChildItem -Path "C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement" -Recurse | ft PSPath

PSPath

Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\en-US
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\ref
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes\win-arm64
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes\win-x64
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes\win-x86
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes\win-arm64\native
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes\win-x64\native
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netCore\runtimes\win-x86\native
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\ref
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes\win-arm64
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes\win-x64
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes\win-x86
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes\win-arm64\native
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes\win-x64\native
Microsoft.PowerShell.Core\FileSystem::C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\netFramework\runtimes\win-x86\native

Expected behavior

Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck

Will uninstall the Module and delete the Folders of the Module

Actual behavior

Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck

throws Acces to the path is denied and Folders are not deleted

Error details

get-error

Exception             :
    Type    : System.ArgumentException
    Message : Parent directory 'C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0' could not be deleted: Access to the path
'\\?\C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement\3.7.0\en-US' is denied.
    HResult : -2147024809
TargetObject          : Microsoft.PowerShell.PSResourceGet.Cmdlets.UninstallPSResource
CategoryInfo          : PermissionDenied: (Microsoft.PowerShel…UninstallPSResource:UninstallPSResource) [Uninstall-PSResource], ArgumentException
FullyQualifiedErrorId : ErrorDeletingDirectory,Microsoft.PowerShell.PSResourceGet.Cmdlets.UninstallPSResource
InvocationInfo        :
    MyCommand        : Uninstall-PSResource
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 9
    Line             : Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck
    Statement        : Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck
    PositionMessage  : At line:1 char:1
                       + Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUse …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Uninstall-PSResource
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
      0
      1

Environment data

Get-Module Microsoft.PowerShell.PSResourceGet; $PSVersionTable

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Binary     1.1.0                 Microsoft.PowerShell.PSResourceGet  {Compress-PSResource, Find-PSResource, Get-InstalledPSResource, Get-PSResourceRepository…}

Key   : PSVersion
Value : 7.5.0
Name  : PSVersion


Key   : PSEdition
Value : Core
Name  : PSEdition


Key   : GitCommitId
Value : 7.5.0
Name  : GitCommitId


Key   : OS
Value : Microsoft Windows 10.0.26100
Name  : OS


Key   : Platform
Value : Win32NT
Name  : Platform


Key   : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0…}
Name  : PSCompatibleVersions


Key   : PSRemotingProtocolVersion
Value : 2.3
Name  : PSRemotingProtocolVersion


Key   : SerializationVersion
Value : 1.1.0.1
Name  : SerializationVersion


Key   : WSManStackVersion
Value : 3.0
Name  : WSManStackVersion

Visuals

Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck
Image

Get-ChildItem -Path "C:\Users\a.bohren\OneDrive - Icewolf\Dokumente\PowerShell\Modules\ExchangeOnlineManagement" -Recurse | ft PSPath
Image

Tried to figure out with Sysinternals ProcessMonitor
Image

Activity

  1. nogaff commented on Feb 18, 2025

    @nogaff

    I've just encountered the exact same issue in both PowerShell 7.4.6 and Windows PowerShell 5.1, using Microsoft.PowerShell.PSResourceGet 1.1.0.

    With PowerShell it specifically complains about access being denied to subdirectories of the module being uninstalled, but I've found that if you manually delete any subdirectories before running the uninstall command, (just leaving behind the files in the module's root), then the command actually seems to succeed.

    However, with Windows PowerShell it more generically complains about the module's root directory not being empty, and even if you manually delete the subdirectories before running the uninstall command, it still fails.

    I've also tried running the command as Administrator and that makes no difference.

  2. BohrenAn commented on Feb 21, 2025

    @BohrenAn
    Author

    I would say, that this did allready happen with Microsoft.PowerShell.PSResourceGet 1.0.6 and i hoped it would be fixed with Microsoft.PowerShell.PSResourceGet 1.1.0.

  3. BohrenAn commented on Mar 7, 2025

    @BohrenAn
    Author

    The same still happens with Microsoft.PowerShell.PSResourceGet 1.1.1

  4. site-speed commented on Mar 26, 2025

    @site-speed

    I have the same issue as well (with Microsoft.PowerShell.PSResourceGet 1.1.1)
    previously used to work OK - I think it started with version 1.0.6 or 1.0.5 - I don't recall exactly

    the same thing happened with PowerShellGet and there was fixed in v 3.0.0-beta-19

  5. site-speed commented on Apr 19, 2025

    @site-speed

    I have the same issue as well (with Microsoft.PowerShell.PSResourceGet 1.1.1) previously used to work OK - I think it started with version 1.0.6 or 1.0.5 - I don't recall exactly

    the same thing happened with PowerShellGet and there was fixed in v 3.0.0-beta-19

    Also unable to catch this exception - so can't really handle it nicely 😢

  6. BohrenAn commented on Apr 29, 2025

    @BohrenAn
    Author

    Any updates here Sydney Smith (@SydneyhSmith) ?

  7. SydneyhSmith commented on Apr 30, 2025

    @SydneyhSmith
    Collaborator

    We are looking into debugging it-- will put in our upcoming quality sprint

  8. BohrenAn commented on May 6, 2025

    @BohrenAn
    Author

    Great to hear that. Thank you Sydney Smith (@SydneyhSmith)

  9. site-speed commented on May 6, 2025

    @site-speed

    In case it helps:
    I'm currently working around this issue with code along these lines:

    try {
    
        Uninstall-PSResource $oldResource -SkipDependencyCheck -ErrorAction Stop
    
    }
    
    catch [System.ArgumentException] {
    
        $FullyQualifiedErrorId = $error[0].FullyQualifiedErrorId
    
        if ($FullyQualifiedErrorId -eq "ErrorDeletingDirectory,Microsoft.PowerShell.PSResourceGet.Cmdlets.UninstallPSResource") {
    
            $resourceDir = Join-Path -Path (Split-Path -Path $PROFILE -Parent) -ChildPath "Modules"
    
            $resourcePath = Join-Path -Path (Join-Path -Path $resourceDir -ChildPath $resource.Name) -ChildPath $oldResource.Version
    
            Write-Host -ForegroundColor Red "Uninstall failed - directly delete delete $resourcePath"        
    
            Remove-Item -Path $resourcePath -Recurse -Force
    
        }
    
    }
    
  10. BohrenAn commented on Jun 28, 2025

    @BohrenAn
    Author

    Still happens with: Microsoft.PowerShell.PSResourceGet 1.2.0-preview1

  11. BohrenAn commented on Jul 22, 2025

    @BohrenAn
    Author

    Still happens with: Microsoft.PowerShell.PSResourceGet 1.2.0-preview2

  12. m-hanisch commented on Sep 11, 2025

    @m-hanisch

    In case it helps: I'm currently working around this issue with code along these lines:

    try {
    
        Uninstall-PSResource $oldResource -SkipDependencyCheck -ErrorAction Stop
    
    }
    
    catch [System.ArgumentException] {
    
        $FullyQualifiedErrorId = $error[0].FullyQualifiedErrorId
    
        if ($FullyQualifiedErrorId -eq "ErrorDeletingDirectory,Microsoft.PowerShell.PSResourceGet.Cmdlets.UninstallPSResource") {
    
            $resourceDir = Join-Path -Path (Split-Path -Path $PROFILE -Parent) -ChildPath "Modules"
    
            $resourcePath = Join-Path -Path (Join-Path -Path $resourceDir -ChildPath $resource.Name) -ChildPath $oldResource.Version
    
            Write-Host -ForegroundColor Red "Uninstall failed - directly delete delete $resourcePath"        
    
            Remove-Item -Path $resourcePath -Recurse -Force
    
        }
    
    }
    

    Tim Moss (@site-speed)
    Can you further elaborate where you made these adjustments?

  13. site-speed commented on Sep 12, 2025

    @site-speed

    @site-speed Can you further elaborate where you made these adjustments?

    ah - apologies, yes of course

    my code snippet is from a script I've written that loops through all installed resources and tries to update them
    (I should publish it to PSGallery but haven't got around to it)

    by default I have modules installed in CurrentUser scope, and have "Documents" redirected to OneDrive, which means that whichever machine I use my PowerShell environment has the same profile scripts, same modules etc.

    however - as described in this Issue Uninstall-PSResource sometimes fails in this scenario when modules are in OneDrive

  14. BohrenAn commented on Sep 13, 2025

    @BohrenAn
    Author

    Hi All,

    Still happens with Microsoft.PowerShell.PSResourceGet 1.2.0-preview3.

    But Pull Request #1860 is still open.

    Image

    Kind Regards
    Andres

  15. alerickson commented on Sep 15, 2025

    @alerickson
    Member

    Hi Andres Bohren (@BohrenAn), unfortunately we were not able to get the fix in for this release, but we will have a preview4 that this should make it into.

  16. secretworkpersona commented on Oct 28, 2025

    @secretworkpersona

    In case it helps: I'm currently working around this issue with code along these lines:

    try {
    
        Uninstall-PSResource $oldResource -SkipDependencyCheck -ErrorAction Stop
    
    }
    
    catch [System.ArgumentException] {
    
        $FullyQualifiedErrorId = $error[0].FullyQualifiedErrorId
    
        if ($FullyQualifiedErrorId -eq "ErrorDeletingDirectory,Microsoft.PowerShell.PSResourceGet.Cmdlets.UninstallPSResource") {
    
            $resourceDir = Join-Path -Path (Split-Path -Path $PROFILE -Parent) -ChildPath "Modules"
    
            $resourcePath = Join-Path -Path (Join-Path -Path $resourceDir -ChildPath $resource.Name) -ChildPath $oldResource.Version
    
            Write-Host -ForegroundColor Red "Uninstall failed - directly delete delete $resourcePath"        
    
            Remove-Item -Path $resourcePath -Recurse -Force
    
        }
    
    }
    

    If this is not allowed, mods please remove. I modified the snippit to be used standalone. Many thanks for the original.

    $oldResource = Get-PSResource 'MicrosoftTeams' -Version 7.3.1
    try {
      Uninstall-PSResource $oldResource -SkipDependencyCheck -ErrorAction Stop
    }
    catch [System.ArgumentException] {
      $FullyQualifiedErrorId = $error[0].FullyQualifiedErrorId
      if ($FullyQualifiedErrorId -eq "ErrorDeletingDirectory,Microsoft.PowerShell.PSResourceGet.Cmdlets.UninstallPSResource") {
        $resourceDir = Join-Path -Path (Split-Path -Path $PROFILE -Parent) -ChildPath "Modules"
        $resourcePath = Join-Path -Path (Join-Path -Path $resourceDir -ChildPath $oldResource.Name) -ChildPath $oldResource.Version
        Write-Host 'Uninstall failed - directly delete:' -ForegroundColor Red
        Write-Host $resourcePath -ForegroundColor Red
        Remove-Item -Path $resourcePath -Recurse -Force
      }
    }
    
    
    
    
  17. anamnavi commented on Nov 7, 2025

    @anamnavi
    Member

    Andres Bohren (@BohrenAn) secretworkpersona (@secretworkpersona) preview 4 has just been released with the fix linked on this issue. If you could test and provide feedback, would be appreciated thanks!

  18. BohrenAn commented on Nov 8, 2025

    @BohrenAn
    Author

    Hi Anam Navied (@anamnavi)

    Thank you for releasing preview 4 including the fix.

    Seems that it's still not fixed for PowerShell 5
    Image

    It seems to work for PowerShell 7
    Image

    A little more Background:
    I am the maintainer of https://www.powershellgallery.com/packages/M365PSProfile.
    Source Code https://github.com/fabrisodotps1/M365PSProfile

    A PowerShell Module to update the most important M365 PowerShell Modules. Checks on Startup (via PowerShell Profile) if new PowerShell Modules are availavle and updates them. We love Microsoft.PowerShell.PSResourceGet because it's mutch more performant and it's does support install / uninstall with Scopes. We uninstall the old Modules to do some housekeeping, before installing a new one.
    Uninstall-Module still missing the -Scope Parameter.

    Image

    I've added a Parameter -FileMode to uninstall Modules (delete the Module Folder) - this works in 95% of the cases

    $ModulesPath = Get-M365ModulePath -Scope $Scope
    Get-ChildItem -Path $ModulesPath -Filter "$Module" -Recurse | Remove-Item -Force -Recurse

    Image

    Kind Regards
    Andres

  19. site-speed commented on Nov 9, 2025

    @site-speed

    I've installed the latest (preview) version, but all PowerShell modules are currently up to date - so can't really tell if there's any change/improvement

    In case it helps:

    I've noticed that when this error happens often the leaf files within the folders have been deleted successfully, so all that remains is an empty directory tree.

    if I browse to this location in Windows Explorer and try to delete the top level folder (version number of the module) then

    if I select the folder an press delete or use Delete in a context menu I get an error popup

    if I navigate down to the deepest folder and delete that, then up a level delete that and so on I can eventually delete the empty directory tree

    again if it helps

    • this only works if I just use default delete option - i.e. delete to recycle bin
      the opting to permanently delete fails stating file/folder in use by another process
      often the OneDrive icon/status shows "syncing" even though files and folders are already synced

    It appears that when a folder is in OneDrive the delete functions appear to check OneDrive status

    • delete to Recycle bin seems more forgiving but permanently delete just fails full stop
      • perhaps a timing issue, or OneDrive not releasing some kind of lock/handle fast enough

    This mainly happens when my script has updated a module like "Az" or "Microsoft.Graph" just examples -not exlusive

    • which then updates a load of sub-modules
    • which in turn triggers OneDrive into updating changes
      the script then tried to update all modules including those sub-modules already updated by (Az or Microsotf.Graph) and hits the issue described
    • potentially OneDrive already processing the deletes

    If I browse to the folders in question in Windows Explorer and try to manually delete that's when I see the behaviour described above

  20. site-speed commented on Nov 18, 2025

    @site-speed

    Hi Anam Navied (@anamnavi)

    Thank you for releasing preview 4 including the fix.

    Since the comment above I have had the chance to run my module updating script and some modules needed updating.
    I had the same experience as Andres Bohren (@BohrenAn)

    With PowerShell 7 it seemed to work fine.

    With PowerShell 5 the issue persists.

    Hope this helps,

    Kind Regards,

    Tim

  21. o-l-a-v commented on May 21, 2026

    @o-l-a-v
    Contributor

    This still happens in 1.3.0-preview1 according to Andres Bohren (@BohrenAn). Reopen?

  22. site-speed commented on May 21, 2026

    @site-speed

    I noticed somewhat recently that the PowerShell 5 version suggests trying again in PowerShell 7 due to a limitation of the version of .NET that PowerShell 5 uses - quite nice!

    I still get an issue with this but there's a definite improvement since early versions

    worth noting is the fact that some of this may not be an issue with this particular PowerShell module but more with how deletes are handled in a OneDrive synced folder

    I don't have hard evidence/logs to hand but I have noticed that in a OneDrive folder

    • Shift + Delete is more likely to fail than just Delete (i.e. send to recycle bin)
    • deleting subfolders and files depth first sometimes helps
    • particularly with PowerShell modules, despite running Remove-Module or Remove-Module -Force, the system still insists they are "in use by another process" - EVEN when they have been "deleted" and moved to the Recycle Bin!

    This particular module is not good at tidying up partly deleted folders left behind from older versions of modules

  23. BohrenAn commented on May 21, 2026

    @BohrenAn
    Author

    I've introduced -FileMode in my M365PSProfile M365PSProfile to simply delete the Folder
    https://www.powershellgallery.com/packages/M365PSProfile/0.9.0

    That solves the Problem in PowerShell 5.1

    If ($FileMode -eq $true)
    {
        Write-Host "Using FileMode. Remove all $Module Modules" -ForegroundColor Yellow
        $ModulesPath = Get-M365ModulePath -Scope $Scope
        Get-ChildItem -Path $ModulesPath -Filter "$Module" -Recurse | Remove-Item -Force -Recurse
    }
    
  24. site-speed commented on May 21, 2026

    @site-speed

    Andres Bohren (@BohrenAn) nice idea! I may add that to my script that I use for updating all modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions