Hi All<\/p>\n
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<\/p>\n
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\n\n<\/code><\/pre>","upvoteCount":7,"answerCount":5,"datePublished":"2023-01-27T16:52:44.000Z","author":{"@type":"Person","name":"Glenn-Maxwell","url":"https://community.spiceworks.com/u/Glenn-Maxwell"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
You’re close:<\/p>\n
$Users = Import-Csv -Path \"C:\\temp\\input.csv\"\n$Users | ForEach-Object {\n Get-ADUser -Filter \"samaccountname -eq '$($_.sname)' -and enabled -eq 'true'\" | Select-Object samaccountname, displayname, UserPrincipalName\n} | Export-Csv -Path \"C:\\temp\\output.csv\" -NoTypeinformation\n<\/code><\/pre>\nThe 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.<\/p>","upvoteCount":2,"datePublished":"2023-01-27T17:00:00.000Z","url":"https://community.spiceworks.com/t/get-aduser/945072/3","author":{"@type":"Person","name":"saidbrandon","url":"https://community.spiceworks.com/u/saidbrandon"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hi All<\/p>\n
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<\/p>\n
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\n\n<\/code><\/pre>","upvoteCount":7,"datePublished":"2023-01-27T16:52:44.000Z","url":"https://community.spiceworks.com/t/get-aduser/945072/1","author":{"@type":"Person","name":"Glenn-Maxwell","url":"https://community.spiceworks.com/u/Glenn-Maxwell"}},{"@type":"Answer","text":"Do you get output when you run just the following code:<\/p>\n
import-csv C:\\temp\\input.csv | Get-ADUser -Filter \"samaccountname -eq '$($_.sname)' -and enabled -eq 'true'\"\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-01-27T16:58:30.000Z","url":"https://community.spiceworks.com/t/get-aduser/945072/2","author":{"@type":"Person","name":"kevinweller","url":"https://community.spiceworks.com/u/kevinweller"}},{"@type":"Answer","text":"$inactiveusers = Import-Csv \"C:\\temp\\input.csv\"\nForeach($user in $inactiveusers){\n Get-ADUser -identity $_.samaccountname -Filter {Enabled -eq $false} | Select-Object samaccountname, displayname, UserPrincipalName | Export-Csv C:\\temp\\output.csv -NoTypeinformation -Append\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-01-27T17:04:37.000Z","url":"https://community.spiceworks.com/t/get-aduser/945072/4","author":{"@type":"Person","name":"andytechacct","url":"https://community.spiceworks.com/u/andytechacct"}},{"@type":"Answer","text":"the other guy beat me to it.
<\/p>","upvoteCount":2,"datePublished":"2023-01-27T17:05:20.000Z","url":"https://community.spiceworks.com/t/get-aduser/945072/5","author":{"@type":"Person","name":"andytechacct","url":"https://community.spiceworks.com/u/andytechacct"}}]}}