Here’s a sample PnP PowerShell script to remove all shared links for one user:
Function Remove-OneDriveSharingLink {
param (
$OneDriveURL
)
process {
Connect-PnPOnline -Url $OneDriveURL
$Ctx= Get-PnPContext
$Files= Get-PnPListItem -List "documents"
foreach( $File in $Files) {
$Froles= $File.RoleAssignments
$Ctx.load($Froles)
$Ctx.ExecuteQuery()
If($Froles.Count -gt 0) {
for ($i = $Froles.Count -1; $i -ge 0 ; --$i){
$Link=$Froles[$i].Member
$Ctx.Load($Link)
$Ctx.ExecuteQuery()
If($Link.title -like "SharingLinks*") {
$Froles[$i].DeleteObject()
}
$Link = $null
}
$Ctx.ExecuteQuery()
}
}
}
}
Remove-OneDriveSharingLink -OneDriveURL "https://<tenant>-my.sharepoint.com/personal/<username>_<tenant>_onmicrosoft_com"
It doesn’t actually remove the links from Shared by you list, but it breaks all the links shared, internal or external.
Note: You need to have access to the user’s personal OneDrive or just use the user’s credential to avoid Access Denied error.
If you no longer need to share files using these blocked accounts, you can run the command below to disable external sharing features for them. It removes all external sharing links as well.
Set-SPOSite https://<tenant>-my.sharepoint.com/personal/<username>_<tenant>_onmicrosoft_com -SharingCapability Disabled
References:
Remove External Shared links in OneDrive for Business using PowerShell.
Office 365 SharePoint/ODFB External User Sharing Management FAQs.