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?

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,188 questions
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 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.