Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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?

like image 210
Tim Sloyan Avatar asked Jul 16 '10 16:07

Tim Sloyan


People also ask

How do I find the assembly version of a dll?

If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there. In File Explorer when you right click the dll file and select properties there is a "File Version" and "Product Version" there.

How do I find the PowerShell assembly version?

You can get file version of file using PowerShell Get-Command cmdlet. In the above command, Get-Command get dll assembly file version of the specified file using FileVersionInfo. FileVersion property.

What is the difference between assembly version and assembly file version?

AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.


2 Answers

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 
like image 187
knut Avatar answered Sep 19 '22 13:09

knut


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 
like image 35
Joey Avatar answered Sep 20 '22 13:09

Joey