Search-mailbox querystring help

Copper Contributor

Powershell Help

I have a script that will read information from a CSV file and will delete emails in O365 using the search-mailbox with the delete content switch.
I am getting different results and I think it is a problem with my query string. The email to delete is found using the email address, subject and the date and time sent. This should result in only one email found. What is happening is when I test the script the logs say more emails are found. I have output the query string using write-host then applied it manually to the search mailbox command and only get one result. I am struggling to figure out why I get different results.

#Version 1.0 Initial Script
write-host "`n"
write-host "Welcome to the GDPR Erasure Tool Version 1.2 `n"

$ConnectToO365 = read-host "Press C to connect to O365 `n"

if ($ConnectToO365 -eq "c") {
#Connect to O365
$Cred = Get-Credential -Credential
$sessionOption = New-PSSessionOption -SkipRevocationCheck
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Cred -Authentication Basic -AllowRedirection -SessionOption $sessionOption
Import-PSSession $session
Connect-MsolService -Credential $cred
elseif ($ConnectToO365 -ne "c") {
write-host "connecting to O365 Skipped... `n`n"

#Function to pop up the open file dialog box in Windows
Function Get-FileName($initialDirectory)
[System.Reflection.Assembly]::LoadWithPartialName("") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null

function ConToUSDate ($date) {

##This function converts the date and time into US and format it so that the search-Mailbox can use it.
$dateTime = $date -split " "
$USDSplit = $dateTime -split '/'
$usd= $USDSplit[1]+'/'+$USDSplit[0]+'/'+$USDSplit[2]
$Output = $usd+'..'+$datetime[1]

return $output
#ConToUSDate -date '15/06/2018 11:30'

Function escape-query ($queryString){

$queryString -replace ":", "``:"

write-host "You will require a results.csv file from a content search to select the emails to be deleted `n"
write-host "Please select the results.csv `n`n "


$inputfile = Get-FileName
$savePath = $inputfile.Substring(0, $inputfile.lastIndexOf('\')) + "\"

write-host "To Test Select 1`nTo Examine the query strings select 2 `nTo Run the Delete Process select 3`n "

$test = read-host "Option"

if ($test -eq "1") {
write-host "Running Script in test mode...`n"
import-csv $inputfile | ForEach-Object {search-mailbox $_.'Location Name' -searchquery "subject:""$(escape-query -querystring $_.'Subject or Title')"" AND Sent:$(ConToUSDate -date $_.Sent)" -EstimateResultOnly } | Export-Csv -NoTypeInformation -Append -Path "$savePath TestLog.csv"
elseif ($test -eq "3"){
write-host "Running Live Delete...Press ctl C to Cancel`n"
import-csv $inputfile | ForEach-Object {search-mailbox $_.'Location Name' -searchquery "subject:""$(escape-query -querystring $_.'Subject or Title')"" AND Sent:$(ConToUSDate -date $_.Sent)" -DeleteContent -confirm:false } | Export-Csv -NoTypeInformation -Append -Path "$savePath DeleteLog.csv"
elseif($test -eq "2"){
#Show the search string for troubleshooting......
import-csv $inputfile | ForEach-Object {write-host $_.'Location Name' -searchquery "subject:""$(escape-query -querystring $_.'Subject or Title')"" AND Sent:$(ConToUSDate -date $_.Sent)" }

$CloseConnection = Read-Host "Close the O365 Connection? y/n"

if ($CloseConnection -eq "y") {
Write-Host "O365 connection closed..."
Get-PSSession | Remove-PSSession
Write-Host "O365 connection still open..."


Spreadsheet example. It is from the results.csv

Location Name,Subject or Title, Sent, Test Compliance Search, 15/06/2018 11:27:18


0 Replies