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?
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With