How to use PowerShell to remove nonempty folder in OneDrive

Copper Contributor

OS:Microsoft Windows 10.0.19042
OneDrive Version:21.052.0314.0001 (Office 365 A1)

Cannot remove nonempty folder in OneDrive directory


Step to reproduce
1. Launch PowerShell in OneDrive directory


PS C:\Users\MyUserName\OneDrive>


2. Try to use Remove-Item cmdlet to remove a nonempty folder in this directory, for example: the .\test\ folder


PS C:\Users\MyUserName\OneDrive> Remove-Item .\test\



Expected result
Without the -Recurse parameter, PowerShell should return a confirm message, such as


The item at C:\Users\MyUserName\OneDrive\test\ has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):



Actual result
PowerShell return a error message


Remove-Item: Cannot remove item C:\Users\MyUserName\OneDrive\test\: The directory is not empty. : 'C:\Users\MyUserName\OneDrive\test\'



1. PowerShell and Administrator:PowerShell get the same result;
2. If I exit OneDrive process and create a new nonempty folder under OneDrive directory, PowerShell can remove it as normal;
3. CMD can remove the folder successfully, which means I can use 


cmd.exe /C "rd /s test"


in PowerShell to remove the folder too. But I want to accomplish this just by PowerShell cmdlet;
4. Get-ChildItem cmdlet shows that the mode of normal folders (not synced by OneDrive) is 'd'(directory), but the mode of synced folders is 'l'(link). However, "dir" command in CMD shows that both of them are <DIR> type. Is this the keypoint?

1 Reply
Same here. And I get 'Access to the cloud file is denied' in Powershell 5.1.

This also prevents Uninstall-Module from working when modules are installed to the default location under the user's OneDrive folder.