0

I found a really good Powershell code on another post here on Stack Overflow. It's below; it loads .csv data in an .xlsx. It does exactly what I want. Is it possible to do this in CMD? I've been looking for this in CMD equivalent but I only keep finding the powershell version.

### Set input and output path
$inputCSV = "C:\Users\username\Desktop\LSGTransactions.CSV"
$outputXLSX = "C:\Users\username\Desktop\Book1.XLSX"

### Create a new Excel Workbook with one empty sheet
$excel = New-Object -ComObject excel.application 
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)

### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = 
$worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)

### Set the delimiter (, or ;) according to your regional settings
$query.TextFileOtherDelimiter = $Excel.Application.International(5)

### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType  = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1

### Execute & delete the import query
$query.Refresh()
$query.Delete()

### Save & close the Workbook as XLSX. Change the output extension for Excel 
2003
$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()
2
  • 1
    If you already have what you need why changing it? You could even run a Powershell script from a CMD batch file ... Commented Mar 22, 2019 at 17:24
  • Oh...that's a good idea. I know that I can submit CMD code to the place where this code will be called; I wasn't sure how Powershell code would fit in. I didn't think about calling one from the other. Thanks. It would be nice for learning CMD better though. Commented Mar 22, 2019 at 17:28

1 Answer 1

1

Generally COM Objects (Which Excel is in powershell) are not available in CMD

However a workaround could be to call a powershell script from the batch file, and pass the filenames as parameters.

powershell.exe yourscript.ps1

You may also need to set the excecution policy on your system, to enable running PS scripts (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6)

Another kind of language supporting COM could be vbscript which you can run from

cscript.exe script.vbs
Sign up to request clarification or add additional context in comments.

2 Comments

To avoid unexpected output, using powershell -NoLogo -NoProfile -File .\yourscript.ps1 may be helpful.
same with cscript //Nologo script.vbs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.