hide display error - powershell

Nijil Kalli 1 Reputation point
2021-12-12T03:39:31.063+00:00

How to hide error in below script if the value of $output parameter is null or empty.

$output = Get-ADUser -Identity $sam -Properties Displayname, EmailAddress, UserPrincipalName, SamAccountName, mailNickname, ipPhone, DistinguishedName, co, Office, Company, Manager, Title, Description, HomePage, AccountExpirationDate, Enabled, PasswordLastSet, PasswordExpired, LockedOut, badPwdCount, BadLogonCount, CannotChangePassword, extensionAttribute8, extensionAttribute9, EmployeeNumber, EmployeeID, extensionAttribute6, extensionAttribute4, extensionAttribute13 | Select-Object Displayname, EmailAddress, UserPrincipalName, SamAccountName, mailNickname, ipPhone, DistinguishedName, co, Office, Company, Manager, Title, Description, HomePage, AccountExpirationDate, Enabled, PasswordLastSet, PasswordExpired, LockedOut, badPwdCount, BadLogonCount, CannotChangePassword, extensionAttribute8, extensionAttribute9, EmployeeNumber, EmployeeID, extensionAttribute6, extensionAttribute4, extensionAttribute13
if ($output -eq $null -or $output -eq "")
{
Write-Host "No user account found with $sam" -foregroundcolor yellow

    }
    else
    {
        Write-Output $output

    }
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Manu Philip 20,206 Reputation points MVP Volunteer Moderator
    2021-12-12T07:58:08.987+00:00

    If you wanted to ignore something, you can use -erroraction 'silentlycontinue' which will ignore all error messages generated by that command.

    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-12-12T16:08:07.02+00:00

    You can use this form:

    $output | Write-Output
    

    If $output is null it won't be passed through the pipe.

    0 comments No comments

  3. Limitless Technology 39,916 Reputation points
    2021-12-14T20:28:03.927+00:00

    Hello @Nijil Kalli

    you can add the -Erroraction silentlycontinue as an exit:

    Detailed information about its usage:
    https://devblogs.microsoft.com/powershell/erroraction-and-errorvariable/

    Hope this helps with your query,

    --------
    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

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.