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

Solution 1:

Here is a pretty one liner:

Get-ChildItem -Filter *.dll -Recurse | Select-Object -ExpandProperty VersionInfo

In short for PowerShell version 2:

ls -fi *.dll -r | % { $_.versioninfo }

In short for PowerShell version 3 as suggested by tamasf:

ls *.dll -r | % versioninfo

Solution 2:

As an ugly one-liner:

Get-ChildItem -Filter *.dll -Recurse |
    ForEach-Object {
        try {
            $_ | Add-Member NoteProperty FileVersion ($_.VersionInfo.FileVersion)
            $_ | Add-Member NoteProperty AssemblyVersion (
                [Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version
            )
        } catch {}
        $_
    } |
    Select-Object Name,FileVersion,AssemblyVersion

If you only want the current directory, then obviously leave out the -Recurse parameter. If you want all files instead of just DLLs, then remove the -Filter parameter and its argument. The code is (hopefully) pretty straightforward.

I'd suggest you spin off the nasty parts within the try block into separate functions since that will make error handling less awkward here.

Sample output:

Name                                    FileVersion     AssemblyVersion
----                                    -----------     ---------------
Properties.Resources.Designer.cs.dll    0.0.0.0         0.0.0.0
My Project.Resources.Designer.vb.dll    0.0.0.0         0.0.0.0
WindowsFormsControlLibrary1.dll         1.0.0.0         1.0.0.0
WindowsFormsControlLibrary1.dll         1.0.0.0         1.0.0.0
WindowsFormsControlLibrary1.dll         1.0.0.0         1.0.0.0

Solution 3:

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

Solution 4:

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

Solution 5:

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.