If you wanted to ignore something, you can use -erroraction 'silentlycontinue'
which will ignore all error messages generated by that command.
hide display error - powershell
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
3 answers
Sort by: Most helpful
-
Manu Philip 20,206 Reputation points MVP Volunteer Moderator
2021-12-12T07:58:08.987+00:00 -
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.
-
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--