Enabling Exchange Online Archiving for Multiple Users with Powershell

With Office 365 being “the” new way of email, it’s important to remember to keep your inbox quite clean so it still stays quick and speedy.

One of the tools that Microsoft gives us to do this is Exchange Online Archiving.

A new email mailbox is added to Outlook and all email is archived based on retention policies.

These retention policies are controlled centrally and it help administrators choose how much email will essentially remain in a user’s live mailbox. Users can also apply tags to folders in order to choose which retention method they would like to use for a folder.

You simple select the folder as a user and pick the retention policy you wish to use.

Below is a powershell script to turn on In-Place archiving for users on Exchange Online.

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
#Script to enable archive mailboxes for specified users
#Current retention policies in Exchange Online specify x amount of days before committing to archive
#1 Month Delete
#1 Week Delete
#1 Year Delete
#5 Year Delete
#6 Month Delete
#Default 2 year move to archive
#Junk Email
#Never Delete
#Personal 1 year move to archive
#Personal 5 year move to archive
#Personal never move to archive
#Recoverable Items 14 days move to archive

#Edit log file location in log Function below

#Function to write a log file out
function Write-Log
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [Alias("LogContent")]
        [string]$Message,
 
        [Parameter(Mandatory=$false)]
        [Alias('LogPath')]
        [string]$Path='C:\OfficeArchiving\log',
         
        [Parameter(Mandatory=$false)]
        [ValidateSet("Error","Warn","Info")]
        [string]$Level="Info",
         
        [Parameter(Mandatory=$false)]
        [switch]$NoClobber
    )
 
    Begin
    {
        # Set VerbosePreference to Continue so that verbose messages are displayed.
        $VerbosePreference = 'Continue'

        #Append the date to the migration log file
        $FormattedDate = Get-Date -Format "yyyy-MM-dd"
        $Path = $Path + "_" + $FormattedDate + ".log"
    }
    Process
    {
         
        # If the file already exists and NoClobber was specified, do not write to the log.
        if ((Test-Path $Path) -AND $NoClobber) {
            Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
            Return
            }
 
        # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
        elseif (!(Test-Path $Path)) {
            Write-Verbose "Creating $Path."
            $NewLogFile = New-Item $Path -Force -ItemType File
            }
 
        else {
            # Nothing to see here yet.
            }  
     
 
        # Write message to error, warning, or verbose pipeline and specify $LevelText
        switch ($Level) {
            'Error' {
                Write-Error $Message
                $LevelText = 'ERROR:'
                }
            'Warn' {
                Write-Warning $Message
                $LevelText = 'WARNING:'
                }
            'Info' {
                Write-Verbose $Message
                $LevelText = 'INFO:'
                }
            }
         
        # Write log entry to $Path
        $timeStamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
        "$timeStamp $LevelText $Message" | Out-File -FilePath $Path -Append
    }
    End
    {
    }
}
##########################Functions preloaded at the top of the script##########################

#Set test mode on so we can specify users
$testMode = 1

#Specify test users to run the script on - UPN should be supplied
$testUsers = "[email protected]"

#Grab current user credentials
$UserCredential = Get-Credential

#Set the exchange powershell session allowing redirection of commands
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

#Import the Powershell session
Import-PSSession $Session

#Evaluate test mode or not
if ($testMode -eq 1) {
    #Loop through each test user and enable inplace archive
    foreach ($user in $testUsers) {
        $searchResult = Get-Mailbox -ResultSize "unlimited" -Identity $user -Filter {RecipientTypeDetails -eq "UserMailbox"}
        Write-Host "User Archive Status for $user = "$searchResult.ArchiveStatus
       
        if ($searchResult.archiveStatus -eq "Active") {
            Write-Log "Archiving already enabled for $user"

        } else {

            $user | Enable-Mailbox -Archive
            Write-Log " enabled archiving for this user $user"

        }
    }
}
else {
    #Loop through all user mailboxes without Archiving turned on and enable it
    $searchResult = Get-Mailbox -ResultSize "unlimited" -Filter {RecipientTypeDetails -eq "UserMailbox"}
    foreach ($result in $searchResult) {
        if ($result.ArchiveStatus -eq "Active") {
           
            #Write out that archiving is already enabled
            Write-Log "Archiving already enabled for $result"

        } else {

            #Enable mailbox archive for this user
            $result | Enable-Mailbox -Archive
            Write-Log " enabled archiving for this user $result"

        }
    }
    #Moving forwards this could be used for new user accounts, although templates should be setup to enable this to be enabled automatically
    #Get-Mailbox -Filter {ArchiveStatus -Eq "None" -AND RecipientTypeDetails -eq "UserMailbox"} | Enable-Mailbox -Archive

#Close connection to exchange
Remove-PSSession $Session
}

Leave a Reply

Your email address will not be published. Required fields are marked *