How can I get a list of installed SQL Server instances?
Today we’ll talk about how we can get a list of locally installed SQL Server instances on a system. This information is stored in the registry at the following location:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL
EDIT:
If you are looking for 32 bit instances on a 64 bit OS, you will need to look here:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL
All we need to do is query this registry key to get the list of instances installed on a machine. On my personal machine, I have the following instances installed:
SQLEXPRESS
MSSQLSERVER
CRASH
DEBUG
MICROSOFT##SSEE
SQL2005
Below are a few ways we can get this information for 64 bit instances on 64 bit Windows:
Command shell “reg.exe” utility:
reg query "HKLM\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL"
In PowerShell:
Get-ItemProperty 'HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL'
In .NET:
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");foreach (string s in key.GetValueNames())
{
MessageBox.Show(s);
}key.Close();
baseKey.Close();
If you need to read the registry from another computer, then the RegistryKey class has a “OpenRemoteBaseKey” static method that you can use.
- Jay
Comments
- Anonymous
October 11, 2011
What's the situation when there are 64-bit and 32-bit on an x64 system? Do we query WoW6432 for 32-bit and native registry for 64-bit? - Anonymous
October 11, 2011
@Phil - thanks for catching that. I should have put this is for 64 bit instances - running on 64 bit Windows. If you are looking for 32 bit instances on a 64 bit machine, you'll need to look here:HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftMicrosoft SQL ServerInstance NamesSQLI edited the post to note that. - Anonymous
November 11, 2012
this is good, thanks Jay - Anonymous
March 01, 2013
Thank you for answering the question first time, correctly. - Anonymous
October 18, 2013
it just works for SQL2005 or higher... not for SQL2000 - Anonymous
November 13, 2013
That gets all installed instances; is there a way to show which of those are active? I'm running an active-active SQL2008r2 cluster and it'd be handy to know when both instances are running on a single node.