Home > Back-end >  How to export data to CSV in powershell where multiple Classes are used
How to export data to CSV in powershell where multiple Classes are used

Time:08-20

I'm basically trying to extract data from the Windows physical machine, I need all 4 classes to achieve the required data. If I take $SysSummary variable in my terminal it outputs the data from all 4 variables. However when exporting to CSV only the first addition to the array is present. Can anyone suggest how I can get all this data into a CSV and nicely formatted?

Thank you.

#Path variable that can be edited to appropriate location

$sysinfopath = 'PATHGOESHERE'

#Assign our required classes for the system details to a variable for easier access in our compiler.

$SysInfo = Get-CimInstance -ClassName Win32_ComputerSystem
$SysProcessor = Get-CimInstance -ClassName Win32_Processor
$SysDisk = Get-CimInstance -ClassName Win32_DiskDrive
$SysBios = Get-CimInstance -ClassName Win32_BIOS

# Compile the $SysInfo class data into our final summary variable.

$SysSummary = @($SysInfo | Select-Object @{N='Computer Name';E={$SysInfo.Name}}, @{N='Domain Name';E={$SysInfo.Domain}},@{N='Manufacturer';E={$SysInfo.Manufacturer}}, @{N='Model';E={$SysInfo.Model}}, @{N='Memory (GB)';E={[math]::Round($SysInfo.TotalPhysicalMemory/ 1GB)}})

# Compile the $SysProcessor class data into our final summary variable.

$SysSummary  = $SysProcessor | Select-Object @{N='CPU Name';E={$SysProcessor.Name}}, @{N='Core Count';E={$SysProcessor.NumberOfCores}} 

# Compile the $SysDisk class data into our final summary variable.

$SysSummary  = $SysDisk | Select-Object @{N='Serial No';E={$SysDisk.Model}}, @{N='Storage Size';E={[math]::Round($SysDisk.Size/ 1GB)}}

# Compile the $SysBios class data into our final summary variable.

$SysSummary  = $SysBios | Select-Object @{N='Serial No';E={$SysBios.SerialNumber}}

# Export the $SysSummary variable to a CSV, path is set on line 25

$SysSummary | Export-Csv -path $sysinfopath

CodePudding user response:

You can use a PSCustomObject to organize your data and then use it to export to csv in "proper" format:

$SysInfo = Get-CimInstance -ClassName Win32_ComputerSystem
$SysProcessor = Get-CimInstance -ClassName Win32_Processor
$SysDisk = Get-CimInstance -ClassName Win32_DiskDrive
$SysBios = Get-CimInstance -ClassName Win32_BIOS

($SysSummary = [PSCUstomObject]@{
    'Computer Name' = $SysInfo.Name
    'Domain Name'   = $SysInfo.Domain
    'Manufacturer'  = $SysInfo.Manufacturer
    'Model'         = $SysInfo.Model
    'Memory (GB)'   = [math]::Round($SysInfo.TotalPhysicalMemory/ 1GB)
    'CPU Name'      = $SysProcessor.Name
    'Core Count'    = $SysProcessor.NumberOfCores
    'Disk Model'    = $SysDisk.Model
    'Storage Size'  = [math]::Round($SysDisk.Size/ 1GB)
    'Serial No'     = $SysBios.SerialNumber
}) | Export-Csv -Path $sysinfopath -NoTypeInformation

The (..) will turn the variable assignment into an expression allowing the value to pass through back into the pipeline. This does 2 things.

  1. Saves the object to $SysSummary.
  2. Passes the object to Export-Csv.
  • Related