views:

34

answers:

1

Good afternoon,

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

I have this for the assembly version (taken of 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 }

My question is how can make a loop of this so that I can return the assembly version of all files within a directory?

Thanks in advance,

Tim

A: 

As an ugly one-liner:

Get-ChildItem -Filter *.dll -Recurse |
    ForEach-Object {
        try {
            $_ | Add-Member NoteProperty FileVersion ($_.VersionInfo.FileVersion)
            $_ | Add-Member NoteProperty AssemblyVersion (
                [Reflection.Assembly]::LoadFile($_.FullName).GetName().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
Joey

related questions