how to get rid of the response from Exchange when password is invalid and catch error message instead?

Konrad Wojciechowski (Nokia) 40 Reputation points
2024-09-25T15:13:34.14+00:00

I have the following command:

$null = Connect-ExchangeOnline -Credential $cred -PSSessionOption $proxyOptions -ShowBanner:$false -ErrorAction Stop
SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorVariable authError 2> $null

if there is a credential error it prints me:

Error Acquiring Token:

AADSTS50126: Error validating credentials due to invalid username or password. Trace ID:

I want to get rid of this message from the screen as later I return some JSON output from this script and it ruins it. I've checked chatgpt and copilot for answers but without success so far.

I am using:

Import-Module -Name ExchangeOnlineManagement -RequiredVersion 3.1.0

Is there any way to do it?

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,519 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,522 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,711 Reputation points
    2024-09-25T21:30:24.5066667+00:00

    Put a Clear-Host cmdlet after the Connect-ExchangeOnline cmdlet.

    Errors are written to the error stream (STDERR). Have you tried redirecting that stream to the $null device on the Connect-ExchangeOnline cmdlet?

    If there's a credential error, why not correct the credential and try again? What's the point of using the credential if you don't care if the cmdlet fails the authentication?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.