1

I have two powershell cmdlets to generate process report and services report.

eg: Get-process and Get-services

I know export-csv c:\test.csv will export the result to excel sheets.

But I need the result of the first cmdlet to export to the first sheet and the second cmdlet to second sheet of the same csv file.

How do I do this?

4
  • 2
    Short answer: You can't do this using Export-Csv because a .csv is a plain text file that doesn't support multiple sheets like a real Excel workbook. Commented Apr 21, 2012 at 16:09
  • @Filburt Can i export it to excel? or can i append to the existing result set and put it in the same sheet in csv? Commented Apr 21, 2012 at 16:17
  • It should be possible to append the second result set to the same csv - maybe needs tweaking (adding some empty lines) to look nice in Excel. Otherwise you'd have to create a real .xslx workbook and fill in the result sets using Office automation. Commented Apr 21, 2012 at 16:24
  • 1
    lucd.info/2010/05/29/beyond-export-csv-export-xls Commented May 15, 2012 at 19:52

1 Answer 1

3

IMO the best option here is just to use excel automation. E.g. ConvertTo-Excel.ps1:

param (
    [ValidatePattern('\.csv$')]
    [ValidateScript({
        Test-Path $_
    })]
    [string[]]$Path
)

$FullPath = $Path | ForEach-Object {
    (Resolve-Path $_).ProviderPath
}

$First, $Rest = $FullPath

$Excel = New-Object -ComObject Excel.Application

$Book = $Excel.Workbooks.Open($First)

foreach ($Item in $Rest) {
    $Next = $Excel.Workbooks.Open($Item)
    $Next.ActiveSheet.Move($Book.ActiveSheet)
}

$Excel.Visible = $true

You can run above script with .\ConvertTo-Excel.ps1 -Path .\services.csv, .\process.csv, ....csv And have all csv combined, in reversed order.

HTH Bartek

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

Comments

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.