Wednesday, October 12, 2011

Appending data to a list of items in a CSV file via PowerShell

I had a list of local usernames that needed to be appended with the domain name of bc\.  Since I'm learning PowerShell, I thought I'd try to find a solution via PS.  Here is what I did:
1.  Created a CSV file with everyone's name in one column and column header name of Name.
2.  Read up on how to append objects and write the new object out to a file.
3.  Created the script:
       import-csv .\names.csv|foreach -process { out-file -filepath names.txt -append -inputobject ("bc\" + $_.Name)}
4.  The script processes each line of the names.csv column by added bc\ to the existing column object then outputs the data into a new text file named names.txt and places the text file into the same file path as the CSV file.
5.  The result changed a username of BobSmith to BC\BobSmith.

No comments: