Powershell help - export to CSV not working - exporting EXE version number - Help required...

I am struggling to get this code below to output to a CSV file. I know I am doing something wrong.

I need to get a version number from an EXE file on multiple devices.

Thanks in advance all.


** Script Details :**

$ExportPath = “C:\Temp”
$ComputerName = Get-Content “C:\Temp\DeviceList.txt”

foreach ($Computer in $ComputerName){

$filePath = “$computer\c$\Admin\program.exe”
$fileVersion = (Get-Command $filePath).FileVersionInfo.FileVersion

Write-Host “$computer The file version is $fileVersion”
}

Gary,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Shouldn’t that be …

$filePath = "\\$computer\c$\Admin\program.exe"

? :wink:

Why Get-Command? I’d recommend using Get-Item or Get-ChildItem instead.

$fileVersion = (Get-Item $filePath).FileVersionInfo.FileVersion

Instead of Write-Host I’d recommend using proper objects:

$ComputerList = Get-Content 'C:\Temp\DeviceList.txt'
    
$Result =
foreach ($ComputerName in $ComputerList) {
    
    $filePath = '\\{0}\c$\Admin\program.exe' -f $ComputerName
    $fileVersion = (Get-Item $filePath).FileVersionInfo.FileVersion
    
    [PSCustomObject]@{
        ComputerName = $ComputerName
        FileVersion  = $fileVersion
    }
}

$Result
$Result | Export-Csv -Path 'C:\Temp\ResultList.csv' -NoTypeInformation

Write-Host writes data to the Information stream. If you had used Write-Output, your text would have gone to the output stream, and could easily have been captured to a csv file.

Context: about Output Streams - PowerShell | Microsoft Learn

If you only switched Write-Host for Write-Output, your output in the CSV file would only be the string of text, which doesn’t seem as useful. Olaf’s suggestion will turn your output into a true CSV with data in columns. Much more usable.