Hi All

i have samaccount names in the below format in the csv file. some of the users are disabled. i want to import this csv file and pull only enabled samaccount account users . i want the output with samaccountname,displayname,UserPrincipalName. please guide me as syntax is throwing error

import-csv C:\temp\input.csv | Get-ADUser -Filter "samaccountname -eq '$($_.sname)' -and enabled -eq 'true'" | Select-Object samaccountname, displayname, UserPrincipalName | Export-Csv C:\temp\output.csv -NoTypeinformation -Append

7 Spice ups

Do you get output when you run just the following code:

import-csv C:\temp\input.csv | Get-ADUser -Filter "samaccountname -eq '$($_.sname)' -and enabled -eq 'true'"

You’re close:

$Users = Import-Csv -Path "C:\temp\input.csv"
$Users | ForEach-Object {
    Get-ADUser -Filter "samaccountname -eq '$($_.sname)' -and enabled -eq 'true'" | Select-Object samaccountname, displayname, UserPrincipalName
} | Export-Csv -Path "C:\temp\output.csv" -NoTypeinformation

The main issue is that you need to do a foreach on the list of users. I also moved the export to the end so you’re saving one file with all the users instead of each item one by one.

2 Spice ups
$inactiveusers = Import-Csv "C:\temp\input.csv"
Foreach($user in $inactiveusers){
    Get-ADUser -identity $_.samaccountname -Filter {Enabled -eq $false} | Select-Object samaccountname, displayname, UserPrincipalName | Export-Csv C:\temp\output.csv -NoTypeinformation -Append

the other guy beat me to it. :slight_smile:

2 Spice ups