How to Delete Outlook Inbox Subfolder Contents Using Powershell?

4 minutes read

To delete Outlook inbox subfolder contents using PowerShell, you can use the Search-Mailbox cmdlet. First, you need to connect to the Exchange Online PowerShell by running the following command:

1
2
$UserCredential = Get-Credential
Connect-ExchangeOnline -Credential $UserCredential -ShowBanner $false


Next, you can use the Search-Mailbox cmdlet to search and delete the contents of the desired subfolder. For example, to delete the contents of a subfolder named "TestFolder" in the inbox, you can run the following command:

1
Search-Mailbox -Identity user@domain.com -SearchQuery "FolderPath:Inbox/TestFolder" -DeleteContent -Force


Replace "user@domain.com" with the email address of the mailbox you want to delete the contents from, and "TestFolder" with the name of the subfolder you want to delete contents from. The -DeleteContent parameter will delete the contents of the specified subfolder, and the -Force parameter will bypass the confirmation prompt.


It's important to note that the Search-Mailbox cmdlet can have significant consequences if not used carefully, so it's recommended to test the command in a non-production environment before running it in a live environment.


How do I delete emails in a subfolder in Outlook using PowerShell?

You can use the following PowerShell script to delete emails in a subfolder in Outlook:

  1. Open PowerShell and run the following command to create an Outlook application object:
1
$olApp = New-Object -ComObject Outlook.Application


  1. Use the Namespace property of the $olApp object to get the GetNamespace("MAPI") object:
1
$namespace = $olApp.GetNamespace("MAPI")


  1. Use the GetDefaultFolder method of the $namespace object to get the folder you want to delete emails from:
1
$folder = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Folders.Item("SubfolderName")


  1. Use a foreach loop to iterate over the items in the folder and delete them:
1
2
3
4
foreach ($item in $folder.Items)
{
    $item.Delete()
}


  1. Finally, release the Outlook application object by calling the Quit method:
1
$olApp.Quit()


Note: Please make sure to replace "SubfolderName" with the name of the subfolder in which you want to delete emails. Also, be cautious when deleting emails as this operation is irreversible.


How do I delete all emails in a subfolder in Outlook via PowerShell?

You can use the following PowerShell script to delete all emails in a subfolder in Outlook:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Load the Outlook Interop Assemblies
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook"

# Initialize Outlook
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNamespace("MAPI")
$Folder = $Namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Folders.Item("SubfolderName")

# Loop through each item in the folder and delete it
foreach ($Item in $Folder.Items)
{
    $Item.Delete()
}

# Clean up
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Folder) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Namespace) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()


Replace "SubfolderName" with the name of the subfolder you want to delete emails from. This script uses the Outlook Interop assemblies to interact with Outlook and delete all items in the specified subfolder. Make sure to run this script with caution as it will permanently delete all emails in the specified subfolder.


What is the PowerShell command to remove all emails from an Outlook subfolder?

The PowerShell command to remove all emails from an Outlook subfolder is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNamespace("MAPI")
$inbox = $namespace.GetDefaultFolder(6) # 6 represents the inbox folder. Change it to the appropriate folder number if needed

$subfolder = $inbox.Folders | Where-Object {$_.Name -eq "SubfolderName"} # Replace "SubfolderName" with the actual name of the subfolder
$emails = $subfolder.Items

foreach ($email in $emails) {
    $email.Delete()
}


Please note that this script assumes you are connected to the Outlook application and you have appropriate permissions to delete emails from the specified subfolder.


How to delete emails in a specific Outlook subfolder using PowerShell commands?

To delete emails in a specific subfolder in Outlook using PowerShell commands, you can use the following script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Add the Outlook interop assembly
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook"

# Create an instance of the Outlook application
$outlook = New-Object -ComObject Outlook.Application

# Get the Inbox folder
$inbox = $outlook.Session.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)

# Get the specific subfolder you want to delete emails from
$subfolder = $inbox.Folders.Item("Subfolder Name")

# Get all the emails in the subfolder
$emails = $subfolder.Items

# Loop through each email and delete it
foreach ($email in $emails) {
    $email.Delete()
}

# Release COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($email) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($emails) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($subfolder) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($inbox) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null


Replace "Subfolder Name" with the name of the specific subfolder you want to delete emails from. Make sure you have the necessary permissions to delete emails in the folder.


Please note that this script uses Outlook interop assembly, which requires Outlook to be installed on the machine running the script. Also, be careful when using this script as it will permanently delete emails and cannot be undone.

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

To delete all Azure resources with PowerShell, you can use the Remove-AzResourceGroup cmdlet. This cmdlet allows you to specify the name of the resource group that you want to delete, and it will remove all resources contained within that group. Additionally, ...
To launch PowerShell as another user, you can use the Start-Process cmdlet with the -Credential parameter. This allows you to specify the credentials of the user you want to run PowerShell as. Here's an example of how you can do this: Start-Process powersh...
To load a custom PowerShell profile with a single command, you can use the following command: . $PROFILE This command will dot-source (i.e., execute) the current user's PowerShell profile, which can be used to customize your PowerShell environment with fun...
To delete multiple records using REST API in Hibernate, you can send a DELETE request with a list of record IDs that you want to delete. In the backend, you can loop through the list of IDs and delete each record using Hibernate's session object. Make sure...
To check an SSH directory in PowerShell, you can use the following command:Get-ChildItem -Path $env:USERPROFILE.sshThis command will list all the files and directories within the .ssh directory in your user profile. You can also navigate to the .ssh directory ...