Repository navigation
Uinstall-PSResource on OneDrive Error: Access to the path is denied #1793
Description
Activity
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.
Reacted by Andres Bohren and Tim MossI 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.
Reacted by Tim MossThe same still happens with Microsoft.PowerShell.PSResourceGet 1.1.1
Reacted by Tim MossI 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 exactlythe same thing happened with PowerShellGet and there was fixed in v 3.0.0-beta-19
Reacted by Andres BohrenI 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 😢
Any updates here Sydney Smith (@SydneyhSmith) ?
SydneyhSmith commented
on Apr 30, 2025 CollaboratorMore actionsWe are looking into debugging it-- will put in our upcoming quality sprint
Great to hear that. Thank you Sydney Smith (@SydneyhSmith)
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 } }Reacted by Andres Bohren and secretworkpersonaStill happens with: Microsoft.PowerShell.PSResourceGet 1.2.0-preview1
Still happens with: Microsoft.PowerShell.PSResourceGet 1.2.0-preview2
Reacted by m-hanischIn 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?@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
Hi All,
Still happens with Microsoft.PowerShell.PSResourceGet 1.2.0-preview3.
But Pull Request #1860 is still open.
Kind Regards
AndresReacted by alericksonHi 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.
Reacted by Andres Bohren and Sam Erdesecretworkpersona commented
on Oct 28, 2025 More actionsIn 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 } }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!
Thank you for releasing preview 4 including the fix.
Seems that it's still not fixed for PowerShell 5

It seems to work for PowerShell 7

A little more Background:
I am the maintainer of https://www.powershellgallery.com/packages/M365PSProfile.
Source Code https://github.com/fabrisodotps1/M365PSProfileA 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.
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
Kind Regards
AndresI'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
- this only works if I just use default delete option - i.e. delete to recycle bin
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
o-l-a-v commented
on May 21, 2026 ContributorMore actionsThis still happens in 1.3.0-preview1 according to Andres Bohren (@BohrenAn). Reopen?
Reacted by Andres Bohren and Tim MossI 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
I've introduced -FileMode in my M365PSProfile M365PSProfile to simply delete the Folder
https://www.powershellgallery.com/packages/M365PSProfile/0.9.0That 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 }Reacted by Tim MossAndres Bohren (@BohrenAn) nice idea! I may add that to my script that I use for updating all modules
Reacted by Andres Bohren
Prerequisites
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
Actual behavior
Error details
Environment data
Visuals
Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck

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

Tried to figure out with Sysinternals ProcessMonitor
