How to delete OneDrive folder and subfolder using powershell?

Aase Nomad 246 Reputation points
2022-02-11T00:27:11.447+00:00

I'm trying to delete OneDrive folder and subfoler (basically everything inside the folder) but I'm not sure how to get it done so I would be really appreciated if I can get any help or suggestion on how to do it.

Connect-SPOService -Url https://company-my.sharepoint.com/     
$OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"  
 
foreach($OneDriveURL in $OneDriveURLs){  
 
Connect-PnPOnline -Url $OneDriveURLs  
Remove-PnPFolder -Name "Test" -Folder "Testing"  
 
}  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,322 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 44,416 Reputation points
    2022-02-11T02:56:26.877+00:00

    Try changing your filter:

    "Url -like '*-my.sharepoint.com/personal/'"
    
    0 comments No comments

  2. MotoX80 31,391 Reputation points
    2022-02-11T14:11:37.827+00:00

    and it's not my actual code

    So post your actual code. Hehe. What folders does it find? What error do you get?

    Or are you just completely lost because that snippet connects to a Sharepoint site, and I don't understand how you got OneDrive into Sharepoint! If you are using the personal OneDrive then this should list your files.

    Get-ChildItem -Path "C:$($env:HOMEPATH)\onedrive" -recurse
    

    Read this site!

    https://evotec.xyz/remove-item-access-to-the-cloud-file-is-denied-while-deleting-files-from-onedrive/

    0 comments No comments

  3. Rich Matheisen 44,416 Reputation points
    2022-08-17T21:30:49.607+00:00

    Instead of writing your own code, have you tried this module? 2.2.8

    It works for both the personal and business version of OneDrive.

    0 comments No comments