Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Outlook client Powershell Script to delete mail including Conversation History (Skype)

$
0
0

Sorry, I'm a newbie to powershell, I have found some code and pieced it together to get this far...but this code for outlook 2010 has errors and I don't know how to clear the conversation history

My organisaton have a computer on a generic windows account which is running outlook, the account has been restricted in exchange to only send internal email, however I want to delete all read email in the inbox (leaving unread untouched), delete drafts, delete outbox, delete sent items and delete conversation history, then empty the recycle bin...


Equally, there may be a better way to script this...

Would anyone be able to help?

Thank you in advance, Steve.

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy"RemoteSigned"

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$outlook = new-object -com outlook.application

Get-Process outlook* | Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process –force

#Define folders

sleep 3

$namespace = $outlook.GetNameSpace("MAPI")

$DefaultFolder = $namespace.GetDefaultFolder(6)

$SentFolder = $namespace.GetDefaultFolder(5)

$OutBoxFolder = $namespace.GetDefaultFolder(4)

$DraftFolder = $namespace.GetDefaultFolder(16)

sleep 3

$DeletedItem=$namespace.GetDefaultFolder(3)

$deletedMail=$DeletedItem.Items

$emails = $DefaultFolder.Items

$Semails = $SentFolder.Items

$Oemails = $OutBoxFolder.Items

$Demails = $DraftFolder.Items

Write-Host -ForegroundColor Green "Name of Folder is $($DefaultFolder.Name)"

Write-host -ForegroundColor Green "Total item in Mail $($emails.Count)"

Write-Host -ForegroundColor Green "Total items for $($DefaultFolder.UnReadItemCount)"

Write-Host -ForegroundColor Red "Total items in $($SentFolder.Name)"

Write-Host -ForegroundColor Red "Total items in $($Semailsr.Count)"

Write-Host -ForegroundColor Red "Total items in $($OutBoxFolder.Name)"

Write-Host -ForegroundColor Red "Total items in $($Oemail.count)"

Write-Host -ForegroundColor Red "Total items in $($DraftFolder.Name)"

Write-Host -ForegroundColor yellow "Name of Folder is $($DeletedItem.Name)"

Write-host -ForegroundColor yellow "Total item in Mail $($deletedMail.Count)"

 $($Semails).delete()

 $($Oemails).delete()

 $($Demails).delete()

}

For($i=($emails.count-1);$i -ge 0;$i--){

if($emails.count -eq 1){

if(! $($emails).Unread){

 $($emails).delete()

}

}

else{

if(! $($emails)[$i].Unread -and $i -ge 0){

 $($emails)[$i].delete()

}

}

if($deletedMail.Count -gt 0){

For($j=($deletedMail.count-1);$j -ge 0;$j--){

if($deletedMail.count -eq 1){

 $($deletedMail).delete() {

else{

 $($deletedMail)[$j].delete()

}

}

}

}



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>