Capture Warning Output in Exchange Online PowerShell

Updated:

Exchange Online PowerShell can display warnings as shown below.

Japanese warning message displayed by Set-Mailbox in Exchange Online PowerShell

To capture the warning in a script, use the WarningVariable parameter available on each command.

1
2
$Warn = ""
Set-Mailbox ExoUser01 -WarningVariable Warn

You can then read the variable specified by WarningVariable to get the warning output.

Example showing a Japanese warning message captured in WarningVariable

As a side note, Exchange Online previously used Remote PowerShell for connections. With that connection, this method could not capture warning output from Exchange Online PowerShell. At the time, the command itself had to run through Invoke-Command, with WarningVariable used to capture the warning. For example:

1
2
$warn = ""
Invoke-Command -Command {Get-Mailbox -ResultSize 1} -Session:(Get-PSSession) -WarningVariable warn

Exchange Online now uses REST for connections, so this method can no longer be used.

Advertisement