Powershell script to import csv and to export file with samaccountname

CiscoAzn
CiscoAzn used Ask the Experts™
on
Powershell import a csv file that already has first name, last name, and email address and need to add another column for samaccountname.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Receiving errors. You don't have a closing bracket and it's also giving me Cannot convert the "-properties" value of type.
Timothy McCartneySYS ADMINISTR I INFRAS

Commented:
Do you just need an additional column added with no actual data?

For example... You feed the script a csv file with the headers you've mentioned, and then it outputs an identical csv file with an additional, empty samaccountname field?

Or... Are you wanting to capture the samaccountname data as a part of the script and export all the data into a new csv file? Your question is a tad unclear on what you're trying to accomplish.

Author

Commented:
Yes that's correct export out with another column that include the samaccountname. sorry for the confusion.
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Post your script
System Engineer
Commented:
You can use the below script to get the SAM account name. UserName in the script should match the header of the column in your input csv file.

import-module ActiveDirectory

Import-CSV C:\filename.csv | Foreach-Object{

get-aduser -Identity $_.UserName | Select Name, SamAccountName

} | export-csv C:\outputfile.csv -Notypeinformation

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial