Write error msg

In this Script finding samaccount for all users in the csv file.
In this file some user do not have samaccount and how do i write a error msg samaccount does not exist

Finding Samaccount for the given Display names

Import-Csv -Path c:\Temp\Accountextension.csv |
foreach {
Get-ADUser -Filter “DisplayName -eq ‘$($_.user)’” -Properties * | Select SamAccountName,UserPrincipalName,AccountExpirationDate

write-host " Fetching the detais '$($_.user)"
}

Venky,
Welcome back to the forum. :wave:t4: … long time no see.

You use an

statement to check if the property has a value before you use the value for the AD query.

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: