Forum Discussion
DarrenRD
Nov 01, 2020Copper Contributor
foreach and out-file- no data
Hi, I need to return a list of active users: Display name etc. but no data is exported to my out-file. Any clues would be highly appreciated . Thanks $users = Get-Content "C:\dump\username...
farismalaeb
Nov 02, 2020Iron Contributor
Hi
What are you expecting from the out-file, where is the inputObject, at least you will need to pipeline it with another command.
if you are talking about dumping the console output to a file, then this is a console redirection, read about it here.
anyway
$users = Get-Content "C:\usernames2.txt"
foreach ($user in $users)
{
try{
$Value=Get-ADUser -ldapfilter "(samaccountname=$user)" -Properties displayname,mail,distinguishedName -ErrorAction stop| Select-Object -Property displayname,mail,distinguishedName
if ($Value.count -eq 0){
Add-Content "C:\namenotfound.txt" "$($user)"
}
else{
add-Content -Path 'C:\UserFound.txt' -Value $Value
}
}
catch{
Write-Host $_.exception.message
}
}
Check the code above