So I have been working on a script that will import a list of GivenName and Surname to search AD for the User accounts of people displaying the table so I can confirm these are the people I want and then export a list of those users SamAccountName to a second csv. Here is the scripting I have as of now.<\/p>\n
$users = Import-Csv 'C:\\Work\\NameSearch.csv'\n \nforeach ($user in $users) {\n$fname = $user.GivenName\n$lname = $user.Surname\n\n \nGet-ADUser -Filter {GivenName -eq $fname -and Surname -eq $lname} -Properties * | \nselect Name,GivenName,Surname,Company,Department,office,StreetAddress,City,State,PostalCode,telephonenumber,mail |\nSelect-Object samaccountname | \nExport-Csv 'C:\\work\\userid.csv' -NoTypeInformation\n}\n<\/code><\/pre>\n
Advertisement
Now, when I use it as just a name search, it displays the information just fine, but when I try to export the sameaccountnames to the userid.csv it only writes the last userid from the list to the csv and I am stumped as to why. I am still learning Powershell so any clues as to what I can look at here would be greatly appreciated.<\/p>","upvoteCount":6,"answerCount":5,"datePublished":"2021-06-08T19:55:24.000Z","author":{"@type":"Person","name":"mcnaujp","url":"https://community.spiceworks.com/u/mcnaujp"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
avoid using asterisk ( * ) with the properties.<\/p>\n
e.g.<\/p>\n
$users = Import-Csv 'C:\\Work\\NameSearch.csv'\n\n$userReport = \nforeach ($user in $users) {\n $fname = $user.GivenName\n $lname = $user.Surname\n \n \n Get-ADUser -Filter {GivenName -eq $fname -and Surname -eq $lname} -Properties Name,GivenName,Surname,Company,Department,office,StreetAddress,City,State,PostalCode,telephonenumber,mail | \n select-object Name,GivenName,Surname,Company,Department,office,StreetAddress,City,State,PostalCode,telephonenumber,mail\n}\n\n# not needed will just show you what you are looking at\n$userReport\n\n$userReport |\nselect-object samaccountname |\nexport-csv 'C:\\work\\userid.csv' -NoTypeInformation\n\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2021-06-08T19:59:30.000Z","url":"https://community.spiceworks.com/t/exporting-samaccountname-to-a-csv/802171/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"So I have been working on a script that will import a list of GivenName and Surname to search AD for the User accounts of people displaying the table so I can confirm these are the people I want and then export a list of those users SamAccountName to a second csv. Here is the scripting I have as of now.<\/p>\n
$users = Import-Csv 'C:\\Work\\NameSearch.csv'\n \nforeach ($user in $users) {\n$fname = $user.GivenName\n$lname = $user.Surname\n\n \nGet-ADUser -Filter {GivenName -eq $fname -and Surname -eq $lname} -Properties * | \nselect Name,GivenName,Surname,Company,Department,office,StreetAddress,City,State,PostalCode,telephonenumber,mail |\nSelect-Object samaccountname | \nExport-Csv 'C:\\work\\userid.csv' -NoTypeInformation\n}\n<\/code><\/pre>\nNow, when I use it as just a name search, it displays the information just fine, but when I try to export the sameaccountnames to the userid.csv it only writes the last userid from the list to the csv and I am stumped as to why. I am still learning Powershell so any clues as to what I can look at here would be greatly appreciated.<\/p>","upvoteCount":6,"datePublished":"2021-06-08T19:55:24.000Z","url":"https://community.spiceworks.com/t/exporting-samaccountname-to-a-csv/802171/1","author":{"@type":"Person","name":"mcnaujp","url":"https://community.spiceworks.com/u/mcnaujp"}},{"@type":"Answer","text":"
If you have Export-Csv inside your ForEach loop, then the CSV file will be overwritten with each iteration. That is why you got only the last account in the file. Neally’s code uses a variable to contain the data from the loop and then outputs it to CSV once at the end. That is the most efficient way to do it.<\/p>","upvoteCount":4,"datePublished":"2021-06-08T20:23:32.000Z","url":"https://community.spiceworks.com/t/exporting-samaccountname-to-a-csv/802171/3","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"
It’s inefficient but had you put -append after your -NTI in the loop, it would have worked too.<\/p>","upvoteCount":1,"datePublished":"2021-06-08T21:36:02.000Z","url":"https://community.spiceworks.com/t/exporting-samaccountname-to-a-csv/802171/4","author":{"@type":"Person","name":"gary-m-g","url":"https://community.spiceworks.com/u/gary-m-g"}},{"@type":"Answer","text":"
Wow, guys I figured it wouldn’t take long to find what I was screwing up but wow you guys are better than I had guessed. I hate to admit I have been banging my head on this with different variations for a little bit now. Thank you all.<\/p>\n
@alexw<\/a> @techadmin8<\/a> @garygreenberg<\/a><\/p>","upvoteCount":0,"datePublished":"2021-06-09T11:32:54.000Z","url":"https://community.spiceworks.com/t/exporting-samaccountname-to-a-csv/802171/5","author":{"@type":"Person","name":"mcnaujp","url":"https://community.spiceworks.com/u/mcnaujp"}}]}}