Search code examples
powershellcsvexport-to-csv

Save names and file-versions to csv (Powershell)


I have folders with some .exe files, where i want to get the names and fileversions. I can get these, but i have problems to save them in a nice .csv format.

Now I have:

n1.exe

18.4.0.0

n2.exe

18.4.15.1

n3.exe

18.4.15.1

n4.exe

18.4.15.1

But i want to save it as:

n1.exe;18.4.0.0;

n2.exe;18.4.15.1;

n3.exe;18.4.15.1;

n4.exe;18.4.15.1;

Here is my code. I have no clue how to get it done with the semicolons

$folder1="D:\Service\Test";

$exeNames = Get-ChildItem -Filter *.exe -Path $folder1 -Name

#Programm Versionen
function efaFileVersionInfo ($efaprograms, $folder) {
    foreach ($efaprogram in $efaprograms) {
        echo $efaprogram
        [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$folder\$efaprogram").FileVersion
    }
}


efaFileVersionInfo $exeNames $folder1 | Out-File D:\Service\folder1.csv

Solution

  • So - using a build-in PowerShell functions you can achieve it by using something like this:

    $folder1 = "D:\Service\Test";
    $list = [System.Collections.Generic.List[PSObject]]@();
    $exeNames = Get-ChildItem -Filter *.exe -Path $folder1 -Name;
    
    #Programm Versionen
    function efaFileVersionInfo ($efaprograms, $folder) {
        foreach ($efaprogram in $efaprograms) {
            $version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$folder\$efaprogram").FileVersion;
            if (!([string]::IsNullOrEmpty($version))) { 
                $version = $version.Trim();
            }
            $obj = [PSCustomObject]@{
                FileName = $efaprogram
                FileVersion = $version
            }
            $list.Add($obj);
        }
    }
    
    efaFileVersionInfo $exeNames $folder1;
    $list | Export-Csv -Path C:\Temp\asd.csv -NoTypeInformation -Delimiter ";";
    

    This presents data in this way:

    "FileName";"FileVersion"
    "7z1900-x64.exe";"19.00"
    "azuredatastudio-windows-user-setup-1.27.0.exe";"1.27.0"
    "Docker Desktop Installer.exe";"3.2.2.61853"
    "Git-2.31.1-64-bit.exe";"2.31.1.1"
    "GitHubDesktopSetup.exe";"2.7.2"
    "Greenshot-INSTALLER-1.2.10.6-RELEASE.exe";"1.2.10.6"
    "ideaIC-2021.1.1.exe";
    "kubectl.exe";