PS script for .NET Core Version

Hi all
I need a powershell script that will scan all the exe and dll files and will print their .NETCore version.
It doesnt matter that I do, all I can find is the product version.

What am I doint wrong?

Below is the present script that I have:

# Define the paths to scan
$paths = @("C:\Program Files\Program1", "E:\Program2")

# Define the output CSV file path
$outputCsv = "C:\FileVersions.csv"

# Initialize an array to hold the file information
$fileInfoArray = @()

# Function to get .NET Core version from a file
function Get-DotNetCoreVersion {
    param (
        [string]$filePath
    )
    try {
        $versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath)
        return $versionInfo.ProductVersion
    } catch {
        return "Unknown"
    }
}

# Scan the directories for .dll and .exe files
foreach ($path in $paths) {
    Get-ChildItem -Path $path -Recurse -Include *.dll, *.exe | ForEach-Object {
        $filePath = $_.FullName
        $dotNetCoreVersion = Get-DotNetCoreVersion -filePath $filePath
        $fileInfoArray += [PSCustomObject]@{
            FilePath = $filePath
            DotNetCoreVersion = $dotNetCoreVersion
        }
    }
}

# Export the file information to a CSV file
$fileInfoArray | Export-Csv -Path $outputCsv -NoTypeInformation

Write-Output "File information has been exported to $outputCsv"

Hello, please make sure to use the “Preformatted Text” button on your code so that it is appropriately formatted. You can also wrap your code in 3 backticks if you’re familiar with Markdown formatting.

Done as requested :slightly_smiling_face:

Anyone please?

Well, it looks like the .NET method you’re using is doing what’s it’s supposed to do:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo($filepath)

Here’s Microsoft’s documentation about that method:
FileVersionInfo
I don’t see anything in there that claims that it would return what version of .NET is required to run that executable or dll.

I’m not even sure what exe/dll to test against on my computer, but the response from Mark Walker on this page looks like a different method to achieve this:

Hi,
Thanks for the reference!

They suggest using Notepad to search for text inside files, but that’s not a viable option for me since I need to scan thousands of files. There must be a way to do this using PowerShell. I really hope someone here knows how.

Well not every exe and dll is written in dotNet.

You can call

$binary = [System.Reflection.Assembly]::LoadFrom("C:\Path\to\file.exe")
$binary.Version

If the first line errors out then it’s not using dotNet

2 Likes

@neemobeer
Unfortunately, this does not print the .NET Core version of a file.

It’s in $binary.customattributes