Repository navigation
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 ModuleActual behavior
Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck
throws Acces to the path is denied and Folders are not deletedError 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
1Environment 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 : WSManStackVersionVisuals
Uninstall-PSResource -Name ExchangeOnlineManagement -Scope CurrentUser -SkipDependencyCheck

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


Activity
nogaff commentedon Feb 18, 2025
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.
BohrenAn commentedon Feb 21, 2025
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.
BohrenAn commentedon Mar 7, 2025
The same still happens with Microsoft.PowerShell.PSResourceGet 1.1.1
site-speed commentedon Mar 26, 2025
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
site-speed commentedon Apr 19, 2025
Also unable to catch this exception - so can't really handle it nicely 😢
BohrenAn commentedon Apr 29, 2025
Any updates here Sydney Smith (@SydneyhSmith) ?
SydneyhSmith commentedon Apr 30, 2025
We are looking into debugging it-- will put in our upcoming quality sprint
BohrenAn commentedon May 6, 2025
Great to hear that. Thank you Sydney Smith (@SydneyhSmith)
site-speed commentedon May 6, 2025
In case it helps:
I'm currently working around this issue with code along these lines:
BohrenAn commentedon Jun 28, 2025
Still happens with: Microsoft.PowerShell.PSResourceGet 1.2.0-preview1
BohrenAn commentedon Jul 22, 2025
Still happens with: Microsoft.PowerShell.PSResourceGet 1.2.0-preview2
m-hanisch commentedon Sep 11, 2025
Tim Moss (@site-speed)
Can you further elaborate where you made these adjustments?
site-speed commentedon Sep 12, 2025
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
BohrenAn commentedon Sep 13, 2025
Hi All,
Still happens with Microsoft.PowerShell.PSResourceGet 1.2.0-preview3.
But Pull Request #1860 is still open.
Kind Regards
Andres
alerickson commentedon Sep 15, 2025
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.
secretworkpersona commentedon Oct 28, 2025
If this is not allowed, mods please remove. I modified the snippit to be used standalone. Many thanks for the original.
anamnavi commentedon Nov 7, 2025
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!
BohrenAn commentedon Nov 8, 2025
Hi Anam Navied (@anamnavi)
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/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.
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
Andres
site-speed commentedon Nov 9, 2025
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
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
This mainly happens when my script has updated a module like "Az" or "Microsoft.Graph" just examples -not exlusive
the script then tried to update all modules including those sub-modules already updated by (Az or Microsotf.Graph) and hits the issue described
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
site-speed commentedon Nov 18, 2025
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
o-l-a-v commentedon May 21, 2026
This still happens in 1.3.0-preview1 according to Andres Bohren (@BohrenAn). Reopen?
site-speed commentedon May 21, 2026
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
This particular module is not good at tidying up partly deleted folders left behind from older versions of modules
BohrenAn commentedon May 21, 2026
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
site-speed commentedon May 21, 2026
Andres Bohren (@BohrenAn) nice idea! I may add that to my script that I use for updating all modules