Get file version and assembly version of DLL files in the current directory and all sub directories

Viewed 57507

I would like to be able to get the file version and assembly version of all DLL files within a directory and all of its subdirectories. I'm new to programming, and I can't figure out how to make this loop work.

I have this PowerShell code to get the assembly version (taken from a forum):

$strPath = 'c:\ADMLibrary.dll'
$Assembly = [Reflection.Assembly]::Loadfile($strPath)

$AssemblyName = $Assembly.GetName()
$Assemblyversion = $AssemblyName.version

And this as well:

$file = Get-ChildItem -recurse | %{ $_.VersionInfo }

How can I make a loop out of this so that I can return the assembly version of all files within a directory?

6 Answers

Let Select-Object create the properties

Get-ChildItem -Filter *.dll -Recurse | Select-Object Name,@{n='FileVersion';e={$_.VersionInfo.FileVersion}},@{n='AssemblyVersion';e={[Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version}}

And Sample output is similar

Name                                           FileVersion AssemblyVersion
----                                           ----------- ---------------
CI.EntityFramework.Initialization.dll          1.0.0.0     1.0.0.0
Castle.Core.dll                                3.3.0.43    3.3.0.0
Castle.Windsor.dll                             3.3.0.51    3.3.0.0
Mutare.VitalLink.dll                           1.0.0.0     1.0.0.0
Newtonsoft.Json.dll                            9.0.1.19813 9.0.0.0

Here's a pretty one-liner:

Get-ChildItem -Filter *.dll -Recurse | ForEach-Object `
{
    return [PSCustomObject]@{
        Name = $_.Name
        FileVersion = $_.VersionInfo.FileVersion
        AssemblyVersion = ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
    }
}

Sample output:

Name            FileVersion AssemblyVersion
----            ----------- ---------------
Minimatch.dll   1.1.0.0     1.1.0.0
VstsTaskSdk.dll 1.0.0.0     1.0.0.0

Based on Joey's answer, but exploiting some handy behaviour for implicit exception handling. First add an extension property:

Update-TypeData -TypeName System.IO.FileInfo -MemberType ScriptProperty -MemberName AssemblyVersion -Value { [Reflection.AssemblyName]::GetAssemblyName($this.FullName).Version }

That can optionally be placed into your profile for reuse. Then the actual selection is just e.g.

Get-ChildItem -Filter *.dll -Recurse | Select-Object Name,AssemblyVersion

As a side-note, the main reason I'm posting this as an additional answer is for the benefit of PowerShell noobs like myself: it took me a long time to figure out that $_ in Joey's answer needs to be turned into $this in the definition given to Update-TypeData.

$j = 'C:\Program Files\MySQL\Connector ODBC 8.0\' # this is the path of foler where you want check your dlls 
$files = get-childitem $j -recurse -include *.dll # this is the command thatwill check all the dlls in that folder 

foreach ($i in $files) {
   $verison = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($i).FileVersion
   Write-Host  "$i ----> $verison "
} # loop is used where it will travel throuhg all the files of the specified folder and check the verion and will print it 
Related