Check whether user exist in AD or Not and write Out-file based on results

Hi,

trying to write a script in powershell which will check whether the user exist in AD or not , the script works fine but the output doesn't put the username if not found 

does not exist in AD () ==>Empty 

User found in AD (CN=XXXX,OU=Users,OU=XX,OU=XX,DC=XX,DC=XXX,DC=com)

Script content:
$users = get-content D:\temp\AllUsers.txt

$result = foreach ($user in $users) {

$User = Get-ADUser -Filter {(samaccountname -eq $user)} 

If ($user -eq $Null) {"does not exist in AD ($user)" }

Else {"User found in AD ($user)"} 

$result | Out-File -FilePath D:\Temp\Allusers-result1.log

i am doing something wrong but cannot figure out .

 

Parents Reply Children
No Data