To make my previous script even more useful I need the final output files to be in a .xls format(yes I know that is not the current excel format but it is the only one that will upload correctly to the site that it is needed for) I can get a file out put that has the data but it is not in the correct rows and columns and I also have some numbers with a leading 0 and tags that look like dates that need to be preserved as they are. I think that I may need to convert my file before I export it as a .xls so this does not happen, but at this stage I am not entirely sure.
Data when I display it at the cmd prompt
$Info
Student ID : 987654
First Name : Fred
Surname : Brown
Unique ID : 0123456
Tag : 3/4/5
Student ID : 876543
First Name : John
Surname : Brown
Unique ID : 234567
Tag : 3k
Do I need to convert my current file and what is the proper export cmd?
If you’re doing this for a single file it’s not worth the pain of scripting it. The excellent Excel module which I’d usually recommend won’t preserve the formatting that you require and, as far as I know, only exports to XLSX.
Your best bet is to open a blank Excel workbook, click the Data tab, then use the From Text wizard to import your CSV file. On the last page of the wizard, change the column data format from General to Text (either for all columns or on a per column basis) and this will preserve your values.
I found a variant of your recommended script that works with Powershell version 4 and now I need to force all the cells to be in text formatting so it does not change the data on me which at the moment makes it unusable.
There are a couple of ways you can try. I tried this:
$xl = New-Object -ComObject "Excel.Application"
$xl.Visible=$true
$wb = $xl.Workbooks.Open("C:\Users\rob\desktop\archive\test.csv")
#Select first worksheet
$ws = $wb.worksheets.Item(1)
#Create a range for everything used in Excel
$usedRange = $ws.UsedRange
#Remove all formatting
$usedRange.ClearFormats()
$wb.SaveAs("C:\Users\rob\desktop\archive\test.xls", 1)
$xl.Quit()
The only thing that was odd about this post was they wanted to call the PS1 with BAT file, so you can just specify the file versus a script to call a script: