1

I tried this code as follows:

param(
$inputPath = "N:\Disease_Prevention\....\Samplename.xlsx",
$outputPath = "N:\Disease_Prevention\...\Output.csv"
)

$xlCSV=6
$inputPath = (Resolve-path $inputPath).Path
$outputpath = (Resolve-path $outputpath).Path
get-childitem $inputPath -File | foreach {
    write-host "processing $_ " 
    $Excelfilename = $_.fullname
    if(!$outputPath)
    {
        $outputPath = $_.DirectoryName
    }
    $CSVfilename =join-path $outputpath $_.BaseName
    $CSVfilename+=".csv";
   
    #open excel and save 
    $Excel = New-Object -comobject Excel.Application
    $Excel.Visible = $False
    $Excel.displayalerts=$False
    $Workbook = $Excel.Workbooks.Open($ExcelFileName)
    $Workbook.SaveAs($CSVfilename,$xlCSV)
    $Excel.Quit()
    If(ps excel){
      kill -name excel
    }
}

And received the following error as follows:

enter image description here

The error says "Unable to get the SaveAs property of the Workbook class"

Would you know why it could be?

And if it's a permission issue, then would you recommend an alternative way to convert excel into csv? Should I use cmdlet to read each line at the excel file and convert one by one?

0

1 Answer 1

1

The issues I encountered with your code are

$outputpath = (Resolve-path $outputpath).Path

Presumably this CSV file doesn't exist, so when you try to resolve the path it gives you nothing.

if(!$outputPath)
{
    $outputPath = $_.DirectoryName
}
$CSVfilename =join-path $outputpath $_.BaseName
$CSVfilename+=".csv";

If you don't pass in an outputpath argument, then this will build the file properly. However, if you pass in an outputpath as a csvfile, then you build out a path that looks like

"N:\Disease_Prevention\...\Output.csv\inputpath basename.csv"

as you're appending the current file's basename and .csv to an already full path.

The adjusted code below worked fine.

param(
    $inputPath = "N:\Disease_Prevention\....\Samplename.xlsx",
    $outputPath = "N:\Disease_Prevention\...\Output.csv"
)

$xlCSV=6
$inputPath = (Resolve-path $inputPath).Path

get-childitem $inputPath -File | foreach {
    write-host "processing $_ " 
    $Excelfilename = $_.fullname
    if(!$outputPath)
    {
        $outputPath = $_.DirectoryName
        $CSVfilename = join-path $outputpath $_.BaseName
        $CSVfilename+=".csv"
    }
    else{
        $CSVfilename = $outputPath
    }

    #open excel and save 
    $Excel = New-Object -comobject Excel.Application
    $Excel.Visible = $False
    $Excel.displayalerts=$False
    $Workbook = $Excel.Workbooks.Open($ExcelFileName)
    $Workbook.SaveAs($CSVfilename,$xlCSV)
    $Excel.Quit()
    If(ps excel){
        kill -name excel
    }

}

I also encourage you to check out Doug Finke's ImportExcel powershell module. Makes working with Excel files so much easier and it doesn't require Excel be installed on the machine that runs the script.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.