What Powershell Query can I Use to Add Email Addresses to an eDiscovery Search as Data Sources

Rich 0 Reputation points
2025-06-23T21:09:44.4133333+00:00

Hi There,

I'm trying to figure out how to automate some eDiscovery work. I have a case established in Purview. Let's call it 'Test Case'. I also have a search preconfigured with KeyQL. I need to add the data sources. I need to add appx. 1900 email addresses to the search. I have all of them in a local text file. Can I use Powershell to script those 1900 email addresses as data sources, as the tenant has ten's of thousands of data sources?

For instance something along the lines of:

$case = Test Case

$search = Test Case Search Name

$addressFile = "C:\testcaseaddressfile.txt"

foreach ($address in (Get-Content $addressFile)) {

Something here with a Set which leverages the pre-existing case name and specified search name....

}

Microsoft Security Microsoft Purview
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2025-06-23T21:28:39.4+00:00

    Install and connect to the Compliance Center:

    Install-Module ExchangeOnlineManagement
    Connect-IPPSSession
    

    Next, you'll need to

    • Identify the case and search.
    • Load the email addresses from the text file.
    • Use the Set-ComplianceSearch cmdlet to update the search's data sources (email addresses in this case).
    • Set-ComplianceSearch's –ExchangeLocation parameter takes a string array of user identities (email addresses, usernames, etc.).

    Note that there’s a 10,000 item limit per search, but fortunately that’s not a concern in your case (you have ~1900). In addition, the update overwrites the ExchangeLocation list—so each time you call Set-ComplianceSearch, it replaces the previous values unless you maintain a growing list in the script.

    # Define variables
    $caseName = "Test Case"
    $searchName = "Test Case Search Name"
    $addressFile = "C:\testcaseaddressfile.txt"
    
    # Read all addresses from file
    $emailAddresses = Get-Content -Path $addressFile
    
    # Convert to array if not already
    $emailArray = $emailAddresses | ForEach-Object { $_.Trim() }
    
    # Update the compliance search
    Set-ComplianceSearch -Identity $searchName -Case $caseName -ExchangeLocation $emailArray
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.