title: How to make this script loop through all files in directory?
body: <p>How can I make this script loop through all the files in the directory?</p>
  
  <p>I believe I got it to save the files the way I want them, but I can do it one at time.</p>
  
  <p>I am learning Powershell...</p>
  
  <p>How can I save each worksheet from the workbook (excel 2010) to this format: file name + "-" + sheet name as CSV?</p>
  
  <ul>
  <li>I have up to 3 worksheets per workbook on some workbooks (might be more...)</li>
  <li>Is there is an optimal way of performing this?</li>
  </ul>
  
  <p>Thank you, </p>
  
  <pre><code>$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
  Add-Type -AssemblyName Microsoft.Office.Interop.Excel
  
  $excel = new-object -ComObject "Excel.Application";
  $excel.DisplayAlerts=$True;
  $excel.Visible =$false;
  
  $wb = $excel.Workbooks.Open($scriptPath + "\1B1195.xlsb");
  
     foreach($ws in $wb.Worksheets) {
      if($ws.name -eq "OP10" -or $ws.name -eq "OP20" -or $ws.name -eq "OP30") {
          Write-Host $ws.name;
  
     $ws.SaveAs($scriptPath + "\" + $wb.name + "-" + $ws.name + ".csv", [Object] [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVMSDOS);
  
  }
  }
  
  $wb.close($False)
  $excel.Quit();
  [void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel);
  </code></pre>