refined PowerShell script for Microsoft Purview eDiscovery (2025 edition). It searches SharePoint Online sites and OneDrives for files containing specific sensitive information:
Hashes
SaaS keys
Passwords
Credentials (numeric sequences longer than 4 but shorter than 20 digits, with special characters and case-sensitive letters)
GUID-like patterns (digits with dashes)
App keys & App passwords Additionally, Preservation Hold Libraries are excluded from the search to prevent unnecessary results from retention policies. Explicit pattern matching for characters, numbers, GUIDs, and security-related keywords ✔ Limits numeric sequences to 5-19 digits, avoiding overly broad results ✔ Preservation Hold Libraries are excluded to focus on actual live data ✔ Refined content search to find characters within files instead of just names
Assistant
Here's a refined PowerShell script for Microsoft Purview eDiscovery (2025 edition). This script is designed to:
✅ Search SharePoint Online and OneDrive locations
✅ Look for sensitive content like hashes, passwords, SaaS keys, app credentials, GUIDs, and patterns matching common secret structures
✅ Specifically matches content within files, not just filenames
✅ Excludes Preservation Hold Libraries to focus on active/live content
✅ Uses refined pattern definitions and filters for effective results without overly broad hits
✅ Finalized & Optimized Script: Purview eDiscovery Sensitive Info Search
powershell
# Connect to Security & Compliance Center
Import-Module ExchangeOnlineManagement
Connect-IPPSSession
# -------------------------- Variables --------------------------
$CaseName = "Sensitive-Search-Case-$(Get-Date -Format yyyyMMddHHmmss)"
$SearchName = "SensitiveInfoSearch"
$Description = "Search for sensitive data patterns & secrets in SharePoint and OneDrive, excluding Preservation Hold Libraries"
# Define Regex-based KQL patterns
# KQL supports partial regex-like search using content match queries
# This KQL targets keywords, hashes, GUIDs, passwords, app keys, etc.
$ContentMatchQuery = @"
(
(
"password" OR "passwd" OR "pwd" OR "secret" OR "api_key" OR "app_key" OR
"client_secret" OR "client_id" OR "access_token" OR "auth_token" OR "token" OR
"private_key" OR "saas_key" OR "connection_string" OR "credential"
)
OR
(
(content:"*[A-Fa-f0-9]{32,}*") /* long hex strings (hashes) */
OR (content:"*[0-9]{5,19}[^A-Za-z0-9]+[A-Za-z]+*") /* 5-19 digit nums with special + letters */
OR (content:"*[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}*") /* GUID pattern */
OR (content:"*sk_live_[A-Za-z0-9]{32,}*") /* OpenAI/Stripe-type secret keys */
OR (content:"*AIza[0-9A-Za-z-_]{35}*") /* Google API keys */
OR (content:"*AKIA[0-9A-Z]{16}*") /* AWS Access Keys */
)
)
"@
# -------------------------- Locations --------------------------
# Retrieve SharePoint sites
Write-Host "[*] Fetching SharePoint Online sites…" -ForegroundColor Cyan
$SPOSites = Get-SPOSite -Limit All | Where-Object {
$_.Title -ne $null -and
$_.Url -notlike "*-hold*" -and
$_.Url -notlike "*preservation*" -and
$_.Template -ne "HOLD#0"
} | Select-Object -ExpandProperty Url
# Retrieve OneDrive personal sites
Write-Host "[*] Fetching OneDrive sites…" -ForegroundColor Cyan
$OneDrives = Get-SPOSite -IncludePersonalSite $true -Limit All |
Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" } |
Select-Object -ExpandProperty Url
# Combine & review total locations
$SearchLocations = $SPOSites + $OneDrives
Write-Host "[+] Total locations to search: $($SearchLocations.Count)" -ForegroundColor Green
# -------------------------- Create eDiscovery Case --------------------------
Write-Host "[*] Creating new eDiscovery case: $CaseName" -ForegroundColor Green
$Case = New-ComplianceCase -Name $CaseName -Description "Automated credential/search case"
# -------------------------- Create the Search --------------------------
Write-Host "[*] Creating content search..." -ForegroundColor Green
New-ComplianceSearch -Case $Case.Name `
-Name $SearchName `
-SharePointLocation $SearchLocations `
-ContentMatchQuery $ContentMatchQuery `
-Description $Description
# Start Search
Start-ComplianceSearch -Identity $SearchName
# -------------------------- Monitor Progress --------------------------
Do {
Start-Sleep -Seconds 20
$SearchStatus = Get-ComplianceSearch -Identity $SearchName
Write-Host "[=] Status: $($SearchStatus.Status) | Items: $($SearchStatus.Items)" -ForegroundColor Yellow
} while ($SearchStatus.Status -eq "InProgress")
# -------------------------- Result Summary --------------------------
$FinalSearch = Get-ComplianceSearch -Identity $SearchName
Write-Host "`n[✓] Search completed!" -ForegroundColor Green
Write-Host " Case: $CaseName"
Write-Host " Search Name: $SearchName"
Write-Host " Items Found: $($FinalSearch.Items)"
Write-Host " Size: $($FinalSearch.Size)"
Write-Host " Status: $($FinalSearch.Status)"
# -------------------------- Optional: Preview or Export Actions --------------------------
# Automatically trigger preview if any results found
if ($FinalSearch.Items -gt 0) {
Write-Host "[*] Creating preview action..." -ForegroundColor Cyan
New-ComplianceSearchAction -SearchName $SearchName -Preview
} else {
Write-Host "[!] No items found, skipping preview." -ForegroundColor Yellow
}
🔒 What This Script Finds:
Common passwords, keys, secrets (password, token, client_secret, etc.)
Long hexadecimal strings (32+ chars) — likely hashes/SaaS/API secrets
Numeric strings (5-19 digits) with special chars and case-sensitive letters (e.g., passwords)