How to use PowerShell to remove nonempty folder in OneDrive

%3CLINGO-SUB%20id%3D%22lingo-sub-2262976%22%20slang%3D%22zh-CN%22%3EHow%20to%20use%20PowerShell%20to%20remove%20nonempty%20folder%20in%20OneDrive%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2262976%22%20slang%3D%22zh-CN%22%3E%3CP%3E%3CFONT%20size%3D%224%22%3EPSVersion%EF%BC%9A7.1.3%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3EOS%EF%BC%9AMicrosoft%20Windows%2010.0.19042%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3EOneDrive%20Version%EF%BC%9A21.052.0314.0001%20(Office%20365%20A1)%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CFONT%20size%3D%225%22%3EDescriptions%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3ECannot%20remove%20nonempty%20folder%20in%20OneDrive%20directory%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3EStep%20to%20reproduce%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3E%201.%20Launch%20PowerShell%20in%20OneDrive%20directory%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EPS%20C%3A%5CUsers%5CMyUserName%5COneDrive%26gt%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3E2.%20Try%20to%20use%20Remove-Item%20cmdlet%20to%20remove%20a%20nonempty%20folder%20in%20this%20directory%2C%20for%20example%3A%20the%20.%5Ctest%5C%20folder%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EPS%20C%3A%5CUsers%5CMyUserName%5COneDrive%26gt%3B%20Remove-Item%20.%5Ctest%5C%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3EExpected%20result%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3EWithout%20the%20-Recurse%20parameter%2C%20PowerShell%20should%20return%20a%20confirm%20message%2C%20such%20as%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EConfirm%0AThe%20item%20at%20C%3A%5CUsers%5CMyUserName%5COneDrive%5Ctest%5C%20has%20children%20and%20the%20Recurse%20parameter%20was%20not%20specified.%20If%20you%20continue%2C%20all%20children%20will%20be%20removed%20with%20the%20item.%20Are%20you%20sure%20you%20want%20to%20continue%3F%0A%5BY%5D%20Yes%20%5BA%5D%20Yes%20to%20All%20%5BN%5D%20No%20%5BL%5D%20No%20to%20All%20%5BS%5D%20Suspend%20%5B%3F%5D%20Help%20(default%20is%20%22Y%22)%3A%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3EActual%20result%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3EPowerShell%20return%20a%20error%20message%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ERemove-Item%3A%20Cannot%20remove%20item%20C%3A%5CUsers%5CMyUserName%5COneDrive%5Ctest%5C%3A%20The%20directory%20is%20not%20empty.%20%3A%20'C%3A%5CUsers%5CMyUserName%5COneDrive%5Ctest%5C'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3ENote%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3E%201.%20PowerShell%20and%20Administrator%3APowerShell%20get%20the%20same%20result%3B%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3E2.%20If%20I%20exit%20OneDrive%20process%20and%20create%20a%20new%20nonempty%20folder%20under%20OneDrive%20directory%2C%20PowerShell%20can%20remove%20it%20as%20normal%3B%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3E3.%20CMD%20can%20remove%20the%20folder%20successfully%2C%20which%20means%20I%20can%20use%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Ecmd.exe%20%2FC%20%22rd%20%2Fs%20test%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%224%22%3Ein%20PowerShell%20to%20remove%20the%20folder%20too.%20But%20I%20want%20to%20accomplish%20this%20just%20by%20PowerShell%20cmdlet%3B%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3E4.%20Get-ChildItem%20cmdlet%20shows%20that%20the%20mode%20of%20normal%20folders%20(not%20synced%20by%20OneDrive)%20is%20'd'(directory)%2C%20but%20the%20mode%20of%20synced%20folders%20is%20'l'(link).%20However%2C%20%22dir%22%20command%20in%20CMD%20shows%20that%20both%20of%20them%20are%20%3CDIR%3E%20type.%3C%2FDIR%3E%20Is%20this%20the%20keypoint%3F%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2262976%22%20slang%3D%22zh-CN%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2722335%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20use%20PowerShell%20to%20remove%20nonempty%20folder%20in%20OneDrive%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2722335%22%20slang%3D%22en-US%22%3ESame%20here.%20And%20I%20get%20'Access%20to%20the%20cloud%20file%20is%20denied'%20in%20Powershell%205.1.%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20also%20prevents%20Uninstall-Module%20from%20working%20when%20modules%20are%20installed%20to%20the%20default%20location%20under%20the%20user's%20OneDrive%20folder.%3C%2FLINGO-BODY%3E
Occasional Visitor

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


Descriptions
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

 

Confirm
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\'

 

 

Note
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.