All scripts
Governance 937

Get-OneDriveSharingLinksReport

Reports anonymous and external sharing links currently active on a user's OneDrive, useful for reviewing exposure before that user leaves the company.

Get-OneDriveSharingLinksReport.ps1
<#
.SYNOPSIS
    Reports active sharing links on a user's OneDrive.

.DESCRIPTION
    Lists anonymous and external sharing links currently active across a
    user's OneDrive files, useful for reviewing external exposure before
    that user's account is disabled or offboarded.

.PARAMETER UserPrincipalName
    UPN of the user whose OneDrive to check.

.EXAMPLE
    .\Get-OneDriveSharingLinksReport.ps1 -UserPrincipalName jane.doe@contoso.com

.NOTES
    Requires PnP.PowerShell.

.AUTHOR
    Shehryar Hassan
#>

param(
    [Parameter(Mandatory)]
    [string]$UserPrincipalName
)

$personalUrl = "https://contoso-my.sharepoint.com/personal/" + ($UserPrincipalName -replace "[@.]", "_")
Connect-PnPOnline -Url $personalUrl -Interactive

$items = Get-PnPListItem -List "Documents" -PageSize 500
$report = foreach ($item in $items) {
    $sharingInfo = Get-PnPFileSharingLink -Identity $item.FieldValues.FileRef -ErrorAction SilentlyContinue
    if ($sharingInfo) {
        foreach ($link in $sharingInfo) {
            [pscustomobject]@{
                File = $item.FieldValues.FileLeafRef
                LinkType = $link.Link.Scope
                LinkUrl  = $link.Link.WebUrl
            }
        }
    }
}

$report | Format-Table -AutoSize
Write-Host "$($report.Count) active sharing link(s) found" -ForegroundColor Cyan

Read it before you run it, and test in a safe tenant first.