-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteOlderEmails.ps1
More file actions
155 lines (150 loc) · 6.95 KB
/
DeleteOlderEmails.ps1
File metadata and controls
155 lines (150 loc) · 6.95 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Param
(
[Parameter(Mandatory = $false)]
[string]$UserName = $Null,
[string]$Password = $Null,
[int]$Days = -1,
[string]$UPN = $Null,
[string]$CSV = $Null
)
Function Connect_Exo {
#Check for EXO v2 module inatallation
$Module = Get-Module ExchangeOnlineManagement -ListAvailable
if ($Module.count -eq 0) {
Write-Host "Exchange Online PowerShell V2 module is not available" -ForegroundColor yellow
$Confirm = Read-Host "Are you sure you want to install module? [Y] Yes [N] No"
if ($Confirm -match "[yY]") {
Write-host "Installing Exchange Online PowerShell module"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
}
else {
Write-Host "EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet."
Exit
}
}
Write-Host Connecting to Exchange Online...
Import-Module ExchangeOnline -ErrorAction SilentlyContinue -Force
#Storing credential in script for scheduling purpose/ Passing credential as parameter - Authentication using non-MFA account
if (($UserName -ne "") -and ($Password -ne "")) {
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName, $SecuredPassword
Connect-ExchangeOnline -Credential $Credential
}
else {
Connect-ExchangeOnline
}
}
function WriteToLogFile ($message) {
$message >> $logfile
}
Function DeleteMail {
$date = (get-date).AddDays( - ($Days)).ToString("MM/dd/yyyy")
Write-Progress "Deleting mails older than $Days days from $UPN mailbox."
$DeleteInfo = Search-Mailbox -Identity $UPN -SearchQuery received<=$date -DeleteContent -Force -ErrorAction Stop -WarningAction SilentlyContinue
$Identity = $DeleteInfo.Identity
$ResultItemsCount = $DeleteInfo.ResultItemsCount
$ResultItemsSize = $DeleteInfo.ResultItemsSize.split("(") | Select-Object -Index 0
$Success=$DeleteInfo.Success
$global:Result = @{'Mailbox Name' = $Identity; 'Deleted mail count' = $ResultItemsCount; 'Deleted mail size' = $ResultItemsSize; 'Result' = $Success }
}
Connect_Exo
Write-Host "Note: Ensure that you have assigned with Mailbox Import Export role." -ForegroundColor Cyan
$global:ExportCSVFileName = "MailDeletionReport_" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
$global:logfile = "MailDeletionLog_" + ((Get-Date -Format "MMM-dd hh-mm-ss tt").ToString()) + ".txt"
if ($UPN -ne "") {
if (($Days -eq -1)) {
[int]$Days = Read-Host "Enter number of days"
try {
DeleteMail
$Results = New-Object PSObject -Property $global:Result
$Results | Format-List 'Mailbox Name', 'Deleted mail count', 'Deleted mail size', 'Success'
}
catch {
Write-Host "Error occured , Please go through your inputs and try again" -ForegroundColor Red
}
}
else {
try {
DeleteMail
$Results = New-Object PSObject -Property $global:Result
$Results | Format-List 'Mailbox Name', 'Deleted mail count', 'Deleted mail size', 'Success'
}
catch {
Write-Host "Error occured , Please go through your inputs and try again" -ForegroundColor Red
}
}
}
elseif ($CSV -ne "") {
if ($Days -eq -1) {
[int]$Days = Read-Host "Enter number of days"
Import-Csv $CSV | ForEach-Object {
$UPN = $_.UPN
try {
DeleteMail
WriteToLogFile "Deletion process done successfully for $UPN mailbox."
$Results = New-Object PSObject -Property $global:Result
$Results | Select-object 'Mailbox Name', 'Deleted Mail Count', 'Deleted Mail Size', 'Result' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force -ErrorAction Stop
}
catch {
WriteToLogFile "Error Occured while deleting mail from $UPN mailbox.Please check the inputs and try again."
}
}
}
else {
Import-Csv $CSV | ForEach-Object {
$UPN = $_.UPN
try {
DeleteMail
WriteToLogFile "Deletion process done successfully for $UPN mailbox."
$Results = New-Object PSObject -Property $global:Result
$Results | Select-object 'Mailbox Name', 'Deleted Mail Count', 'Deleted Mail Size', 'Result' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force -ErrorAction Stop
}
catch {
WriteToLogFile "Error Occured while deleting mail from $UPN mailbox.Please check the inputs and try again."
}
}
}
}
else {
$UPN = Read-Host "Enter the identity of the user"
$Days = Read-Host "Enter number of days"
try {
DeleteMail
$Results = New-Object PSObject -Property $global:Result
$Results | Format-List 'Mailbox Name', 'Deleted Mail Count', 'Deleted Mail Size', 'Result'
}
catch {
Write-Host "Error occured , Please check whether you have necessary permissiosn and go through your inputs" -ForegroundColor Red
}
}
if ((Test-Path -Path $global:logfile) -eq "True") {
if ((Test-Path -Path $global:ExportCSVFileName) -eq "True") {
Write-Host "Deleted email size report availble in `"$global:ExportCSVFileName`"" -ForegroundColor Green
Write-Host "The Logfile is availble in $global:logfile."
$prompt = New-Object -ComObject wscript.shell
$userInput = $prompt.popup("Do you want to open output files?", 0, "Open Output File", 4)
if ($userInput -eq 6) {
Invoke-Item "$global:ExportCSVFileName"
Invoke-Item "$global:logfile"
}
}
else {
Write-Host "The Logfile is availble in $global:logfile."
$prompt = New-Object -ComObject wscript.shell
$userInput = $prompt.popup("Do you want to open output files?", 0, "Open Output File", 4)
if ($userInput -eq 6) {
Invoke-Item "$global:logfile"
}
}
}
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
Write-Host "Disconnected active ExchangeOnline session"
<#
=============================================================================================
Name: Delete older emails in Outlook using PowerShell
Description: This script deletes emails older than x days using PowerShell and exports log file & report to CSV file
Website: m365scripts.com
For detailed script execution: https://m365scripts.com/exchange-online/how-to-delete-older-emails-in-outlook-using-powershell
============================================================================================
#>