Get file version and assembly version of DLL files in the current directory and all sub directories
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 ve开发者_开发问答rsion of all files within a directory?
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
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
精彩评论