How to remove OneDrive folder using PowerShell

Viewed 1582

I'm trying to remove a user OneDrive folder using PowerShell but I'm not seeing any sucess even though I've been searching around internet so I would be really appreciated if I can get any help or suggestion.

so Just for testing purpose, I'm trying to delete my a folder in my own OneDrive called "Testing" and I wanted to delete everything in there including subfolders and files.

    Connect-SPOService -Url https://company-admin.sharepoint.com  
 
    $OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
 
    foreach($OneDriveURL in $OneDriveURLs)
 
    {
 
    Connect-SPOService -Url https://company-admin.sharepoint.com 
 
    Connect-PnPOnline -Url $OneDriveURLs
    Remove-PnPFolder -Name "Google Drive" -Folder "Testing"
 
    }
1 Answers

Your cmdlet format is not correct, you should follow the structure, more about it in Microsoft Docs

You would need to change <username> to the name the personal drive.

Change your format to :

$drive= https://company-admin.sharepoint.com/personal/<username>
$folder = 'testing'
Remove-PnPFolder -Name $drive -Folder $folder

If that does not work, as an alternative you can try the following powershell module OneDrive and use:

Remove-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/Upload"

You can read more about the module on their GitHub page.

Related