66

I have set of scripts for doing scripted installs. You can use the scripts on any server 2008 machine. However, I need to check if .NET 3.5 has been installed (before the scripts run) using a dos batch file. Is that possible?

I know I can check if a file in the C:\WINDOWS\Microsoft.NET\Framework\v3.5 exists, but it would be nice to have something a little more reliable.

I would like to check if it's actually installed, not just if the dir/file exists.

Thanks

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431

9 Answers9

76

Since you said you want to know if its actually installed, I think the best way (short of running version specific code), is to check the reassuringly named "Install" registry key. 0x1 means yes:

C:\>reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5"| findstr Install

   Install     REG_DWORD       0x1
   InstallPath REG_SZ  c:\WINNT\Microsoft.NET\Framework\v3.5\

This also happens to be the "Microsoft Recommended" official method.

WMI is another possibility, but seems impractical (Slow? Takes 2 min on my C2D, SSD). Maybe it works better on your server:

C:\>wmic product where "Name like 'Microsoft .Net%'" get Name, Version

Name                                                Version
Microsoft .NET Compact Framework 1.0 SP3 Developer  1.0.4292
Microsoft .NET Framework 3.0 Service Pack 2         3.2.30729
Microsoft .NET Framework 3.5 SP1                    3.5.30729
Microsoft .NET Compact Framework 2.0                2.0.5238
Microsoft .NET Framework 4 Client Profile           4.0.30319
Microsoft .NET Framework 4 Multi-Targeting Pack     4.0.30319
Microsoft .NET Framework 2.0 Service Pack 2         2.2.30729
Microsoft .NET Framework 1.1                        1.1.4322
Microsoft .NET Framework 4 Extended                 4.0.30319

C:\>wmic product where "name like 'Microsoft .N%' and version='3.5.30729'" get name

Name  
Microsoft .NET Framework 3.5 SP1

Other than these I think the only way to be 100% sure is to actually run a simple console app compiled targeting your framework version. Personally, I consider this unnecessary and trust the registry method just fine.

Finally, you could set up an intranet test site which is reachable from your server and sniffs the User Agent to determine .NET versions. But that's not a batch file solution of course. Also see doc here.

Amit Naidu
  • 2,494
  • 2
  • 24
  • 32
  • 2
    This method doesn't show all .NET Framework versions from GAC. Only 4.0 in my case: *Microsoft .NET Framework 4 Multi-Targeting Pack 4.0.30319*, *Microsoft .NET Framework 4 Extended 4.0.30320*, *Microsoft .NET Framework 4 Client Profile 4.0.30320*. Although 2.0.50727, 3.0.30729.5420, 3.5.30729.5420 versions are also installed. – Mosc Apr 04 '13 at 15:41
  • 1
    I use this one for 4.5: reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"| findstr Version – Guido Leenders May 07 '14 at 19:41
  • 3
    @GuidoLeenders Even better `reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v Version` – gregmac Oct 13 '15 at 16:13
  • `reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"| findstr 4.6` I have to filter only version 4.6. This is how I did it. – Andy Gauge Apr 19 '17 at 18:44
18

You mean a DOS command such as below will do the job displaying installed .NET frameworks:

wmic /namespace:\\root\cimv2 path win32_product where "name like '%%.NET%%'" get version

The following may then be displayed:

Version
4.0.30319

WMIC is quite useful once you master using it, much easier than coding WMI in scripts depending on what you want to achieve.

MacG
  • 201
  • 2
  • 2
  • 3
    I tried this command, verbatim, but it just hung there and froze and gave no response – Aviad Rozenhek May 23 '11 at 08:46
  • Aviad, yes same here. You just have to wait. It took 3 minutes ! – Amit Naidu Jul 29 '11 at 09:33
  • Wow 3 minutes? That long? My virtual machine did it in about 10 seconds. Is there a way to limit it to the latest version install and show path to the location on the drive? – thefoyer Oct 26 '12 at 21:20
  • This method doesn't properly show all .NET Framework versions from GAC. In my case it only shows properly 2.0 & 4.0: *Microsoft ASP.NET MVC 2 2.0.50217.0*, *Microsoft Windows SDK for Visual Studio .NET 4.0 Framework Tools 7.1.30514*, *Microsoft ASP.NET MVC 2 - Visual Studio 2010 Tools 2.0.50217.0*, *Microsoft .NET Framework 4 Multi-Targeting Pack 4.0.30319*, *Microsoft Windows SDK .NET Framework Tools (30514) 7.1.30514*, *Microsoft .NET Framework 4 Extended 4.0.30320*, *Microsoft .NET Framework 4 Client Profile 4.0.30320*. Although 3.0.30729.5420, 3.5.30729.5420 versions are also installed. – Mosc Apr 04 '13 at 15:49
  • It does not hang. It takes a few minutes which is crazy, but I suggest that you use wmic because using registry keys does not always work on all windows. Also, I've noticed that depending on the tool that you are using, on some windows versions, you may not have the registry read permissions on them... – max May 02 '14 at 19:59
  • For .NET 4.6.2 I'm getting: `Version\n1.0.0\n4.6.01590`. Not sure what that `1.0.0` means... Running on Windows Server 2008 R2 – vitaly-t Aug 12 '16 at 08:54
  • 1
    What makes it too long to finish? – Jon P Nov 23 '16 at 07:13
  • Although slow, this method has the advantage that it shows all installed versions of .NET. – Mr. Blonde Sep 06 '17 at 09:01
  • Not the most catchy and easily remembered commands, but it worked for me! Thanks! – Gary Woodfine Nov 30 '17 at 12:20
15

Unfortunately the best way would be to check for that directory. I am not sure what you mean but "actually installed" as .NET 3.5 uses the same CLR as .NET 3.0 and .NET 2.0 so all new functionality is wrapped up in new assemblies that live in that directory. Basically, if the directory is there then 3.5 is installed.

Only thing I would add is to find the dir this way for maximum flexibility:

%windir%\Microsoft.NET\Framework\v3.5
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 4
    Thanks for the help. I think checking for the dir will work just fine. Also, thanks for the reminder about using %windir% –  Jan 30 '09 at 04:30
  • 5
    This simple directory check no longer holds good to detect version. There is no v4.5 directory for .NET Framework 4.5 -http://stackoverflow.com/questions/12070518/where-is-the-net-framework-4-5-directory – mvark Feb 04 '13 at 12:53
  • Check out this MSDN article: [How to: Determine Which .NET Framework Versions Are Installed](https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx#net_d). It explains that you can no longer rely on the directory only. For versions >=4.0 you need to analyze the registry. – Matt Mar 10 '16 at 14:59
  • is there any powershell command like python --version or node--version for net ? – Arash Apr 01 '16 at 19:10
5

You can write yourself a little console app and use System.Environment.Version to find out the version. Scott Hanselman gives a blog post about it.

Or look in the registry for the installed versions. HKLM\Software\Microsoft\NETFramework Setup\NDP

ajma
  • 12,106
  • 12
  • 71
  • 90
  • 1
    Gotta love it - Hanselman's blog has a widget that displays "Now playing: Alicia Keys - If I Was Your Woman/Walk On By" linked to the iTunes store. :D – dkretz Jan 29 '09 at 19:49
  • Only problem is my scripts might be run on a freshly installed vm with nothing but the base OS. I think checking for the dir exist will work for what I need. –  Jan 30 '09 at 04:32
  • If you care about SP1 versus non SP1, you're going to have use something else, like checking the registry. – ajma Jan 30 '09 at 06:42
4

If you're going to run a little console app, you may as well install clrver.exe from the .NET SDK. I don't think you can get cleaner than that. This isn't my answer (but I happen to agree), I found it here.

Community
  • 1
  • 1
0

REM Search for the CONFIG file, if this doesn't exit then the user doesn't have the .Net framework 2.0 `

SET FileName=%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG
IF EXIST %FileName% GOTO INSTALL_DIALER
ECHO.You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
oz123
  • 27,559
  • 27
  • 125
  • 187
0

This is working for me:

@echo off
SETLOCAL ENABLEEXTENSIONS

echo Verify .Net Framework Version

for /f "delims=" %%I in ('dir /B /A:D %windir%\Microsoft.NET\Framework') do (
    for /f "usebackq tokens=1,3 delims= " %%A in (`reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\%%I" 2^>nul ^| findstr Install`) do (
        if %%A==Install (
            if %%B==0x1 (
                echo %%I
            )
        )
    )
)

echo Do you see version v4.5.2 or greater in the list?

pause

ENDLOCAL 

The 2^>nul redirects errors to vapor.

  • 2
    MS has deprecated the 'v4.0' registry folder in favour of a 'v4' folder with an release number in it, like 393297 = 4.6. https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx#net_b – Aaron Nov 13 '15 at 18:21
0

you can check installed c# compilers and the printed version of the .net:

@echo off

for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do (
    set "l="
    for /f "skip=1 tokens=2 delims=k" %%$ in ('"%%# #"') do (
        if not defined l (
            echo Installed: %%$
            set l=%%$
        )
    )
)

echo latest installed .NET %l%

the csc.exe does not have a -version switch but it prints the .net version in its logo. You can also try with msbuild.exe but .net framework 1.* does not have msbuild.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
-2

you can have a look to this page for .NET 4 : http://www.itninja.com/question/batch-script-to-check-and-install-dotnet4-0

  • [Link only answers are discouraged](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). Please provide at least a summary of the information provided in the link. – Scott Chamberlain Oct 15 '15 at 18:37