Outlook Client Powershell script to delete mail including Conversation History (Skype)


I'm a newbie to this site and 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"


$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(olFolderInbox)

$SentFolder = $namespace.GetDefaultFolder(5)

$OutBoxFolder = $namespace.GetDefaultFolder(4)

$DraftFolder = $namespace.GetDefaultFolder(16)

sleep 3




$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)"








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



if($emails.count -eq 1){

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






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




if($deletedMail.Count -gt 0){

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


if($deletedMail.count -eq 1){









  • As for this...

     I'm a newbie to this site and powershell,

    It's OK to be new at X or Y, but one should not just guess at things and hope they will work. So, please spend sometime getting ramped up on PowerShell scripiting, Outlook Object model / programming. You need to do this to limit / eliminate, misconceptions, poor code, bad habits, frustrating errors, destroying your host or you companies environment. Number one rule, never ever run anyone's code, no matter where you get it, unless you fully understand what it is doing. Especially, when you are doing destructive things like this.

    There are tons of free / no cost resources to leverage, all you need to do is invest time. See my Reddit posts here:

    Learning this stuff.

    Best Practices

    There are tons of similar post from amny other fine folks as well. Yet, YouTube, MSDN Channel9 are the go to video locations for you to hit and loo up videos on PowerShell beginning/intermediate/advanced, form design/GUI, Active Directory, Outlook, Exchange, etc...)

    Anyway, this is a good number of things wrong here, and this happens with new folks and copy/paste spaghetti code efforts. Never try and solve all things at once in a script. Do it a piece at a time, to make sure you are getting the results you'd expect. As well as doing so in a non-destructive way. Read up on -WhatIf and -Confirm.

    Why are you doing this Outlook and PowerShell vs using Exchange direclty?
    Use the Excnahge cmdlets, on the Exchange server nativly for this and you don't need to be on the client at all or use Outlook.

    There are several videos on how to do this...

    You can set Exchange polices to auto delete mail and the like as well. See the Exchange docs on that topic.

    As for where you are now...

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

    This only matters if you are starting the script from the DOS cmd line. If you are already in the PowerShellConsole / ISE / VSCode, just run the script.
    If your host ExecutionPolicy is already set to RemoteSigned(and it should be as it's now the default vs Restricted), then there is little need to try and change it to what it already is. If your host is set to Restricted, then this makes sense. Yet again, if you are in a PowerShell session, there is no reason to start another one.

    Why are you loading this?


    This is for WInForm and VB messagebox use case and you have no Form/GUI/MessageBox code anywhere in your post. It is not required for Outlook DOM interop or use.

    This ...

                $outlook = new-object -com outlook.application

    ... is not complete. You do not have the Office names space loaded at all, so this will fail, and any calls to it will fail. So, this instead... ( but please see the references below for details before trying this.)

              ### Messing with Outlook folders

             Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
             $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
             $outlook = new-object -comobject outlook.application
             $namespace = $outlook.GetNameSpace("MAPI")

            ($folder = $namespace.getDefaultFolder)
            $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)

    There are many articles and sample script of how to use
    PowerShell to do thing in Outlook and any Office app. Even posts I've made on this very forum.

            Get-Process outlook*

    When typing the code, and enter the -Name parameter and spacebar, each processname/servicename will popup a list to select one.
    Also, what are you doing the below, you are trying to call Outlook, then shutting it down, and then trying to run interop code.
    There should never be a reason to have multiple copies of Outlook.exe running. If you are calling into Outlook via code, then you should be closing it when you are done and before any subsequent runs.

     sleep 3

    Always use full cmdlet names in scripts and parameters. Short names are great for interactive quick keyboard stuff, but not in scripts.

    So, stuff like..

             Start-Sleep -Seconds 3

    ... instead. Yet, it's a choice. Yet again, see the Best Practices links I show in my references

    PowerShell is explicitly verbose to make code easier to read, understand, maintain and pass on to others or for those who will follow you. A lot of posts online will use shorthand stuff, to save space, and because of their own coding habits.

    None of what you have remaining, will fully work, because you have not set your environment correctly.
    Well, that and the code is not correct. If you are doing this in the ISE/VSCode, those red squiggles are specific errors that must be corrected.

    Please see these  PowerShell with Outlook references
    PowerShell - Managing an Outlook Mailbox with PowerShell