Morning guys,<\/p>\n
I am trying to create a script which Imports a CSV which currently has just got a title ‘users’ and below is just their full names. So for Example<\/p>\n
users
\nJohn Smith
\nDean Winchester<\/p>\n
I want to import it and check AD to see if they exist and if they do, export their SamAccountNames and EmailAddress to another CSV.<\/p>\n
Is this possible? I’ve been trying to figure it out but I am not very good at Powershell to be honest and I’m just getting nowhere.<\/p>\n
Thanks in advance.<\/p>","upvoteCount":7,"answerCount":7,"datePublished":"2022-08-23T05:43:45.000Z","author":{"@type":"Person","name":"jacbos","url":"https://community.spiceworks.com/u/jacbos"},"acceptedAnswer":{"@type":"Answer","text":"
Try something like this<\/p>\n
Foreach ($User in (Import-Csv -Path c:\\temp\\Users.csv))\n\n{\n\n$User = $User.name\n\n Try {\n\n Get-ADUser -Filter {name -eq $User} -Properties EmailAddress | select samaccountname, emailaddress -ErrorAction Stop | Export-Csv c:\\temp\\CorrectUsers.csv -NoTypeInformation -Delimiter \",\" -Append\n\n }\n\n Catch {} # Do something in the catch block with users that dont exist in AD?\n\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2022-08-23T06:59:58.000Z","url":"https://community.spiceworks.com/t/query-csv-and-then-export-if-it-matches/934213/2","author":{"@type":"Person","name":"rickardw","url":"https://community.spiceworks.com/u/rickardw"}},"suggestedAnswer":[{"@type":"Answer","text":"Morning guys,<\/p>\n
I am trying to create a script which Imports a CSV which currently has just got a title ‘users’ and below is just their full names. So for Example<\/p>\n
users
\nJohn Smith
\nDean Winchester<\/p>\n
I want to import it and check AD to see if they exist and if they do, export their SamAccountNames and EmailAddress to another CSV.<\/p>\n
Is this possible? I’ve been trying to figure it out but I am not very good at Powershell to be honest and I’m just getting nowhere.<\/p>\n
Thanks in advance.<\/p>","upvoteCount":7,"datePublished":"2022-08-23T05:43:45.000Z","url":"https://community.spiceworks.com/t/query-csv-and-then-export-if-it-matches/934213/1","author":{"@type":"Person","name":"jacbos","url":"https://community.spiceworks.com/u/jacbos"}},{"@type":"Answer","text":"
Hi,<\/p>\n
Thanks for getting back to me,<\/p>\n
I’ve tried running this script now, changed the csv path etc but when I run it nothing happens and no csv is outputted.<\/p>\n
Doesn’t look like I’m getting an error messages either.<\/p>\n
Any ideas?<\/p>\n
Thanks,<\/p>","upvoteCount":0,"datePublished":"2022-08-23T08:02:10.000Z","url":"https://community.spiceworks.com/t/query-csv-and-then-export-if-it-matches/934213/3","author":{"@type":"Person","name":"jacbos","url":"https://community.spiceworks.com/u/jacbos"}},{"@type":"Answer","text":"
Ignore me, I was the problem.<\/p>\n
Thanks RickardW, sorted it.<\/p>","upvoteCount":1,"datePublished":"2022-08-23T08:46:46.000Z","url":"https://community.spiceworks.com/t/query-csv-and-then-export-if-it-matches/934213/4","author":{"@type":"Person","name":"jacbos","url":"https://community.spiceworks.com/u/jacbos"}},{"@type":"Answer","text":"
When querying AD with the filter parameter, you’ll get a $null response if the user doesn’t exist. PowerShell is also about objects, writing to a csv inside a foreach loop is not a good practice.<\/p>\n
$Users = Import-Csv -Path \"c:\\temp\\Users.csv\"\n\n$Results = $Users | ForEach-Object {\n $ADUser = Get-ADUser -Filter \"name -eq '$($User.Name)'\" -Properties EmailAddress\n\n if ($null -ne $ADUser) {\n $ADUser | Select-Object SamAccountName, EmailAddress\n }\n}\n\n$Results | Export-Csv -Path \"C:\\temp\\CorrectUsers.csv\" -NoTypeInformation\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2022-08-23T09:54:16.000Z","url":"https://community.spiceworks.com/t/query-csv-and-then-export-if-it-matches/934213/5","author":{"@type":"Person","name":"saidbrandon","url":"https://community.spiceworks.com/u/saidbrandon"}},{"@type":"Answer","text":"