Hello! First post here!
I’ve been self teaching myself PowerShell for a few months now. I’ve written a few scripts to manipulate some CSV data and format it different ways. This has to do with a HAM radio database for anyone interested. I treat the files as text files rather than Import-CSV and Export-CSV functions for the most part as I’m working with a 95,000+ line CSV file and that would take a bit. This moves much faster without treating it as a database.
My original CSV file “usersraw.csv” is formatted with 7 columns. Although the raw file is headerless, the columns are as follows: Radio ID,Callsign,Name,City,State,Country,Comment
In the output CSV, it needs to be 7 columns, however I need to remove the last columb and add a new column in the middle, copying the data of another column. I need to omit the “Comment” column, and add a new column called “NickNames” in after “Names”, copying over the “Name” column data to it. The final column, with header information, needs to be arranged like so: Radio ID,Callsign,Name,NickName,City,State,Country"
In my past scripts, I’ve had to add header information to the CSV, perfix the row with a number, and add two blank columns, using quotes for text qualifiers, which was fairly easy. The way I did this was:
(gc -Encoding UTF8 $MyPath\usersraw.csv) | %{$i++;“”“$($i-1)”“,$"} | foreach {$ + “,`"Private Call`”,`"None`”" } | Out-File -Encoding UTF8 “$MyPath\formatted.csv”
“`"No.`”,`"Radio ID`",`"Callsign`",`"Name`",`"City`",`"State`",`"Country`",`"Remarks`",`"Call Type`",`"Call Alert`"" | Out-File -Encoding UTF8 -FilePath “$OutPutFilePath”
Add-Content -Path “$OutPutFilePath” -Value (gc -Encoding UTF8 “$MyPath\formatted.csv”)
Basically I took the raw file and added the row number column up front, appended the two new blank columns, saved that to a file, then wrote a new file with the correct header info, then appended the formatted data to that new file with the header.
I tried using Import-CSV however I cannot see functions to add columns and order them the way I want, only add them to the end. How would you suggest I go about doing this? Ive been stuck on this for a few days and can’t quiet find my way around it. Thanks for any input!