I have an existing script which does the job of checking if a given user exists in AD or not. But I'm unable to export the result in csv file. Please help.
Clear-Host
$UserList = gc .\Output_userInfo.csv
$outputFilePath = "D:\Input\User&Group_Output.csv"
foreach ($u in $UserList) {
try {
$ADUser = Get-ADUser -Identity $u -ErrorAction Stop
}
catch {
if ($_ -like "Cannot find an object with identity: '$u'") {
"User '$u' does not exist." | Export-Csv .\notexists.csv -NoTypeInformation -Force
}
else {
"An error occurred: $_"
} continue
}
"User '$($ADUser.SamAccountName)' exists." |
Export-Csv .\notexists.csv -NoTypeInformation -Force
}
