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
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();
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
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?
@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:
I edited the post to note that.
this is good, thanks Jay
Thank you for answering the question first time, correctly.