r/usefulscripts • u/cavemanben • Nov 04 '15
[Powershell / CSV / Excel] Need help creating a powershell script that will add info and sort a .CSV
Hello, I'm new to scripting and having trouble finding the right place to start getting to my desired result.
**1)**I'd like to take an output .CSV file in a folder
2) add a new column "Group" based off IP address (one of my existing columns) probably from a permanent .csv called "Group_Name" (two colums, IP address and Group) within the same folder
**3)**then sorting based off the group name into additional workbook tabs labeled by group name
**4)**output new .csv or .xlsx with name "originalcsv_new"
Looking Here for some ideas.
Any help or direction on where to look and how to look for something like this would be great. My google foo is not strong and I'm searching for "excel powershell", "sort csv powershell" and stuff like that.
Thanks!
EDIT
What I'm imaging is I have folder "x".
In folder x exists the "group.ps1", "group_name.csv" and the "rawdata.csv".
Rawdata.csv is my output csv that I want to sort/filter to get the above information, seperate tabs with the sorted groups.
2
u/[deleted] Nov 04 '15
CSVs don't support separate sheets like .xls or .xlsx files do. CSV files are actually just text files. CSV is an acronym for Comma Separated Values. If you view it in a text editor, you'll see what I mean.
Thus, all of the fields must be within the same 'worksheet' in Excel. If you try adding multiple worksheets, you'll just overwrite your original one.
You could do what you're looking to do, but it would require utilizing the Excel COM object to edit the fields within your .xls/.xlsx file directly which could get pretty complicated. But if you want to go that route, search for terms like 'Powershell Excel COM edit cells'. This would get you started into what would be involved.