Find .NET runtime versions in Powershell

We are using multiple versions of the .NET framework at a client’s, in a huge enterprise application.
We had an issue today, where someone had introduced a dependency on .NET 4.0 too low down in the stack, so that projects in solutions building after it, would fail with the following error:


C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3258: The primary reference "OurMiddleTierComponent" could not be resolved because it has an indirect dependency on the .NET Framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework. [D:\src\path\projectFile.vbproj]

In the process of trying to find out why it fails (using Reflector, nDepend, etc), I put together the following Powershell snippet to help us investigate which DLLs are 4.0 and which are 2.0.

It requires that you run Powershell in .NET 4.0 if you have any .net 4.0 assemblies in there.

dir -r -i *.dll | % { $version = [System.Reflection.AssemblyName]::GetAssemblyName($_).Version; echo "$($_.Name);$version" } 2> $null | sort > dll-versions.txt

This makes a sorted, semicolon-separated list of dll names and its corresponding runtime version.
The redirect of stderr to $null is to handle the case that you could have the same DLL in multiple folders, then .NET will give an error when you try to load it again.

And then you can use your tool of choice (Excel or whatever) to further investigate.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s