How to export PowerShell output to CSV – Using hash table?

We have many ways to export a PowerShell output to CSV format, and the simple one is just using Export-CSV which is familiar to all… Below is the method of exporting a PowerShell output to CSV format by using a hash table. Here we are updating (Appending) hash table from a For-each loop and exporting the result to CSV format. At the end using Export-CSV to export the hash table values. The benefit is to make out the result as per our customized way.

Scenario: Just pulling few details of a group of machines

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee176825(v=technet.10)?redirectedfrom=MSDN

Get-Process | export-csv C:vivekprocess.csv

Passing the result to a hash table and exporting it to CSV

If we want to export a customized PowerShell output to a CSV file, we can use a hash table to do that. Here I am giving a scenario-based example for creating a CSV output using a hash table.
Scenario: Pinging a group of systems and exporting the result to CSV file.And also checking the Operating system name and RAM size of the system,

Creating an empty hash table to store the result and Getting the Machine Name from a .txt file located on our desktop.
You can use your own way to input the system details.

$details = @()
$pc = Get-Content $env:USERPROFILE\desktop\pc.txt
foreach ($sys in $pc)
{

This script portion will start based on the ping result. If the ping result is false, it just creates a hash table with predefined content. And if the result is true, there are some WMI queries to check the system details.

Forming hash table and adding the values to PSObject based on ping result
Using [ordered]@ table to keep the order as we give
if(!(Test-Connection -Cn $sys -BufferSize 16 -Count 1 -ea 0 -quiet))
{
$Result = [ordered]@{
MACHINE_NAME     = "$sys"
PING_STATUS      = "MACHINE OFFLINE"
OS_NAME = "N/A"
TOTAL_PHYSICALMEMORY = "N/A"
}
$Details += New-Object PSObject -Property $Result
}
We have to create a PSObject to store and append the hash table values. After the for-each loop, we will be exporting this psobject to comma separate value (CSV)
$Details += New-Object PSObject -Property $Result

Ping Checking (Online) –  As I mentioned previously, this is the true portion of ping result. If the ping result is true, we are checking the OS_NAME and TOTAL PHYSICALMEMORY of the system.

$osname = "N/A"
$physicalmemory = "N/A"
$sysdetails = "N/A"
$sysdetails = Get-WmiObject -Class Win32_computersystem -ComputerName $sys | select -ExpandProperty TotalPhysicalMemory
$osname = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $sys | select -ExpandProperty caption
$physicalmemory = ($sysdetails)/1mb -as [int]

Forming hash table and adding the values to PSObject based on ping result

$Result = [ordered]@{
MACHINE_NAME     = "$sys"
PING_STATUS      = "MACHINE ONLINE"
OS_NAME = "$OSNAME"
TOTAL_PHYSICALMEMORY = "$physicalmemory"
}
$Details += New-Object PSObject -Property $Result

At the end exporting all the result to .CSV file. I just created a location path with a customized name. You can give anything which you like

$date = Get-Date -UFormat "%m-%d-%y"
$pathofcsv = "$env:userprofile\desktop\" + "Ping_Result_" + "$date" + ".csv"
$Details | export-csv -Path $pathofcsv -NoTypeInformation

The output will looks like below

Now putting all the script together

#Machine availability check
#Handling Output using CSV

$details = @()
$pc = Get-Content $env:USERPROFILE\desktop\pc.txt
foreach ($sys in $pc)
{
if(!(Test-Connection -Cn $sys -BufferSize 16 -Count 1 -ea 0 -quiet))
{
    $Result = [ordered]@{
MACHINE_NAME     = "$sys"
PING_STATUS      = "MACHINE OFFLINE"
OS_NAME = "N/A"
TOTAL_PHYSICALMEMORY = "N/A"
}
$Details += New-Object PSObject -Property $Result
}
else
{
$osname = "N/A"
$physicalmemory = "N/A"
$sysdetails = ""
$sysdetails = Get-WmiObject -Class Win32_computersystem -ComputerName $sys | select -ExpandProperty TotalPhysicalMemory
$osname = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $sys | select -ExpandProperty caption
$physicalmemory = ($sysdetails)/1mb -as [int]


    $Result = [ordered]@{
MACHINE_NAME     = "$sys"
PING_STATUS      = "MACHINE ONLINE"
OS_NAME = "$OSNAME"
TOTAL_PHYSICALMEMORY = "$physicalmemory"
}
$Details += New-Object PSObject -Property $Result
}
}
$date = Get-Date -UFormat "%m-%d-%y"
$pathofcsv = "$env:userprofile\desktop\" + "Ping_Result_" + "$date" + ".csv"
$Details | export-csv -Path $pathofcsv -NoTypeInformation

vivekrr.com