Hey everyone! I'm new to PowerShell and I'm trying to create a script that collects various information from a single PC, like the CPU, RAM, motherboard details, and installed programs, and I want to export all this data into a CSV file. I've been attempting to do this for about two weeks now and I'm stuck on how to format the output appropriately for a CSV. My current script produces a clean .txt file, but the transition to .csv has been tricky. Here's a snippet of my code:
```powershell
$BIOS = @( Get-CimInstance -ClassName Win32_BIOS |
Select-Object Manufacturer, Name, SerialNumber, Version |
Format-Table -AutoSize
)
$CPU = @( Get-CimInstance -ClassName Win32_Processor |
Select-Object Name, SocketDesignation, MaxClockSpeed |
Format-Table -AutoSize
)
$PROGRAMME = @( Get-ItemProperty HKLM:SoftwareMicrosoftWindowsCurrentVersionUninstall* |
Select-Object DisplayName, Publisher, DisplayVersion
)
$BIOS, $CPU, $PROGRAMME | Out-File -FilePath "C:UsersdamanDesktopLolTest.txt"
$BIOS, $CPU, $PROGRAMME | Export-Csv -LiteralPath "C:UsersdamanDesktopLolTest.csv" -NoTypeInformation
```
Any advice on what I might be doing wrong? Thanks!
3 Answers
You're right to use arrays, but avoid `Format-Table` as it formats the output rather than retains it as structured data that `Export-Csv` can use. Instead, keep your variables like `@($BIOS)`, but don’t wrap them in formatting commands. Just pass them straight to `Export-Csv`. Here's a little tweak:
```powershell
$combined = $BIOS + $CPU + $PROGRAMME
$combined | Export-Csv -Path 'C:UsersdamanDesktopcombined.csv' -NoTypeInformation
```
You'll get all the data in one CSV, but keep in mind they'll be under their respective columns!
Generally, a CSV is meant for tabular data where each column has a consistent type of information. You might consider combining the data under universal column names for your datasets. Here’s a basic example of merging:
```powershell
$allData = $BIOS + $CPU + $PROGRAMME | Select-Object Manufacturer, Name, SerialNumber, SocketDesignation, MaxClockSpeed, DisplayName, Publisher, DisplayVersion
$allData | Export-Csv -Path 'C:UsersdamanDesktopAllData.csv' -NoTypeInformation
```
This creates a unified CSV with all your data, ensuring missing fields are handled nicely with blanks.
It seems like you might be trying to export three separate data sets into a single CSV, which can be tricky. You shouldn't use `Format-Table` before exporting; it doesn’t work well with CSV. Each query should output raw objects instead of formatted ones. You could create three separate CSV files for each dataset, or combine them into a unified format. Check this out:
```powershell
$BIOS | Export-Csv -Path 'C:TempBIOS.csv' -NoTypeInformation
$CPU | Export-Csv -Path 'C:TempCPU.csv' -NoTypeInformation
$PROGRAMME | Export-Csv -Path 'C:TempPrograms.csv' -NoTypeInformation
```
This way, you'll have manageable CSVs.
I prefer having everything in one CSV. Is that achievable with my current setup?

So, if I have properties that don’t match between BIOS and programs, how will that work?