Delete a specific sharepoint external share using powershell

Chris Mancey (Group) 221 Reputation points
2021-09-23T12:05:30.22+00:00

I have a SharePoint library in an O365 tenant with a large number of files/folders. The files have been shared externally with customers over the years and now we have reached to threshold limit for sharing.

We need to remove any shares for external guests that are older than 1 month

Using PowerShell, here's what I have so far

 ###### Declare and Initialize Variables ######    
 $url="<siteURL>"    
 $listName="Sharing Links"    
 $currentTime= $(get-date).ToString("yyyyMMddHHmmss")    
 $logFilePath=".\logs"+$currentTime+".txt"    
 # Fields that has to be retrieved    
 $Global:selectProperties=@("SharingDocId", "AvailableLinks","ShareId","AuthKey","RoleDefinitionId","Invitees","Created");    
 $DaysCutOff = 31  
    
 ## Start the Transcript    
 Start-Transcript -Path $logFilePath  
    
 ## Export List to CSV ##    
 function ExportList    
 {    
     try    
     {    
         # Get all list items using PnP cmdlet    
         $listItems=(Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues  
         $outputFilePath="C:\scripts\results-"+$currentTime+".csv"    
     
         $hashTable=@()    
    
         # Loop through the list items    
         foreach($listItem in $listItems)    
         {     
             $obj=New-Object PSObject                
             $listItem.GetEnumerator() | Where-Object { $_.Key -in $Global:selectProperties }| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}    
             $hashTable+=$obj;    
             $obj=$null;    
             if ($listItem.Created -lt (Get-Date).AddDays(-$DaysCutOff) ){  
                 write-host "Share Older that $DaysCutOff days"  
   
                 #Delete the share <-- not sure what goes here to delete the share  
             }  
         }    
     
         $hashtable | export-csv $outputFilePath -NoTypeInformation    
      }    
      catch [Exception]    
      {    
         $ErrorMessage = $_.Exception.Message           
         Write-Host "Error: $ErrorMessage" -ForegroundColor Red            
      }    
 }    
    
 ## Connect to SharePoint Online site    
 Connect-PnPOnline -Url $url -UseWebLogin    
   
    
 ## Call the Function    
 ExportList    
    
 ## Disconnect the context    
 Disconnect-PnPOnline    
    
 ## Stop Transcript    
 Stop-Transcript    

Not all global variable get values but it works so far i just don't know how to delete the share

If it look at the variable for $listitem I can see the values

134599-image.png

Is it possible to delete base on the ShareingDocID or UniqueID fields?

Any help would be appreciated.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,604 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-09-24T06:19:21.843+00:00

    Hi @Chris Mancey (Group) ,

    You could delete base on the ID filed. Use Remove-PnPListItem cmdlet by passing item id to delete it:

    Remove-PnPListItem -List $listName -Identity "<ID>"  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful