How to Format an Array for Excel Export with PowerShell?

0
15
Asked By CuriousCoder99 On

Hey everyone,

I'm working with a query that generates the following output:

```
Date Title Usage
20251109 DeptA 800000
20251109 DeptB 700000
20251109 DeptC 600000
20251109 DeptD 850000
```

For my Excel export, I need the data to be reformatted into:

`DeptA DeptB DeptC DeptD`
`800000 700000 600000 850000`

I'm using the Export-Excel command from the ImportExcel Module for exporting. How can I manipulate the array to achieve this format for my output? Sorry for the poor formatting, and thanks in advance for your help!

2 Answers

Answered By DataWhiz23 On

Could you share what you've tried so far? It might help narrow down the solution.

CuriousCoder99 -

Not much, just trying to reformat the result like this:

```
$swappedArray = @()
foreach ($item in $resultset) {
$swappedArray += [PSCustomObject]@{
$item.Title = $item.Usage
}
}
```

But the result isn’t quite what I expected.

Answered By ExcelGuru88 On

Have you posted any code? I'm pretty familiar with the Import-Excel module and I think this could be a simple sorting issue.

CuriousCoder99 -

All I’m doing is taking the output from the query above and exporting it like this:

```
$resultset | export-excel "c:tempmyoutput.xlsx" -worksheetname "Usage"
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.