Unable to successfully export Powershell results to CSV

When I execute my PowerShell script in ISE, it completes successfully, but when I add the syntax to export ( |export-csv) the results are blank.

Anyone able to help me out here...?

$computerSystem = Get-CimInstance CIM_ComputerSystem
$computerBIOS = Get-CimInstance CIM_BIOSElement
$computerOS = Get-CimInstance CIM_OperatingSystem
$computerCPU = Get-CimInstance CIM_Processor
$computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'"
$computerName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name)
$computerName1 = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name
$IPAddress = (Test-Connection $computerName1 -count 1).IPv4Address.IPAddressToString
Clear-Host

Write-Host "System Information for: " + $computerSystem.Name
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
"HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
"HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.LastBootUpTime
"ComputerName: " + $computerName.Name
"IP Address: " + $IPAddress  | export-csv c:\results.csv -notype -append

Open in new window

Randy AntesAsked:
Who is Participating?
 
oBdACommented:
Try it like this:
$ComputerSystem = Get-CimInstance CIM_ComputerSystem
$ComputerBIOS = Get-CimInstance CIM_BIOSElement
$ComputerOS = Get-CimInstance CIM_OperatingSystem
$ComputerCPU = Get-CimInstance CIM_Processor
$ComputerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'"

Write-Host "System Information for: " + $ComputerSystem.Name
New-Object -TypeName PSObject -Property ([ordered]@{
	"Manufacturer" =		$ComputerSystem.Manufacturer
	"Model" =				$ComputerSystem.Model
	"Serial Number" =		$ComputerBIOS.SerialNumber
	"CPU" =					$ComputerCPU.Name
	"HDD Capacity" =		"{0:N2}GB" -f ($ComputerHDD.Size/1GB)
	"HDD Space" =			"{0:P2} Free ({1:N2}GB)" -f ($ComputerHDD.FreeSpace/$ComputerHDD.Size), ($ComputerHDD.FreeSpace/1GB)
	"RAM" =					"{0:N2}GB" -f ($ComputerSystem.TotalPhysicalMemory/1GB)
	"Operating System" =	"$($ComputerOS.caption), Service Pack: $($ComputerOS.ServicePackMajorVersion)"
	"User logged In" =		$ComputerSystem.UserName
	"Last Reboot" =			$ComputerOS.LastBootUpTime
	"ComputerName" = 		$ComputerSystem.Name
	"IP Address" =			(Test-Connection $ComputerSystem.Name -count 1).IPv4Address.IPAddressToString
}) | Export-Csv -Path c:\results.csv -NoTypeInformation -Append

Open in new window

3
 
FOXActive Directory/Exchange EngineerCommented:
try the below

"IP Address: " + $IPAddress  | export-csv 'c:\temp\results.csv' -notypeinformation
0
 
Randy AntesAuthor Commented:
The directory isnt the issue, I am actually placing the file on my desktop so I know permissions isnt the cause. It is something to do with the syntax of the over all script. When not exported, it works 100%, but no results when exported (blank csv file is created)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
To explain what happened: You wrote the last string only to the file.
Writing a string with Export-CSV usually results in its length to be written out, if anything.

You always need an object for Export-CSV, a simple data type like integer or string does not work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.