How to delete OneDrive folder and subfolder using powershell?

Aase Nomad 246 Reputation points

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     
$OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like ''"  
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,416 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 45,186 Reputation points

    Try changing your filter:

    "Url -like '*'"
    0 comments No comments

  2. MotoX80 32,326 Reputation points

    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!

    0 comments No comments

  3. Rich Matheisen 45,186 Reputation points

    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