I have written a PowerShell script to delete all emails and inbox subfolders of a secondary mailbox open in my Outlook profile but to leave the rest of a users' mailbox intact. I want to know if there's a way of doing it without sending everything to the Deleted Items folder first.
I am working remotely over a VPN and this unnecessary step of moving files into my Delete Items is both slow and an annoying inconvenience. Is there any script equivalent to Shift+Delete?
Get-Member returns the following list of methods for the mail item object type. I've been using the Delete method but I can't find the object type reference on MSDN to have a closer look at its methods to see if Delete will take a parameter to permanently delete for example.
TypeName: System.__ComObject#{00063034-0000-0000-c000-000000000046}
Name MemberType Definition
---- ---------- ----------
AddBusinessCard Method void AddBusinessCard (ContactItem)
ClearConversationIndex Method void ClearConversationIndex ()
ClearTaskFlag Method void ClearTaskFlag ()
Close Method void Close (OlInspectorClose)
Copy Method IDispatch Copy ()
Delete Method void Delete ()
Display Method void Display (Variant)
Forward Method MailItem Forward ()
MarkAsTask Method void MarkAsTask (OlMarkInterval)
Move Method IDispatch Move (MAPIFolder)
PrintOut Method void PrintOut ()
Reply Method MailItem Reply ()
ReplyAll Method MailItem ReplyAll ()
Save Method void Save ()
SaveAs Method void SaveAs (string, Variant)
Send Method void Send ()
ShowCategoriesDialog Method void ShowCategoriesDialog ()
My script looks like this at the moment:
# Initialise variables
$TargetMailbox = "Mailbox - User"
# SCRIPT BODY
# ===========
# Connect to Outlook
$Outlook = New-Object -comobject Outlook.Application
# Select the mailbox by name
$Mailbox = $Outlook.Session.Folders | Where-Object { $_.Name -eq $TargetMailbox}
# Select Inbox by name
$Inbox = $Mailbox.Folders | Where-Object { $_.Name -eq "Inbox" }
# Delete subfolders
$Inbox.Folders | foreach {
write-host "Deleting Inbox\$($_.Name) (Folder)"
$_.Delete()
}
# Delete items
$Inbox.Items | foreach {
write-host "Deleting Inbox\$($_.subject) (Item)"
$_.Delete()
}