Skip to content

Instantly share code, notes, and snippets.

@sqlchow
Last active August 29, 2015 14:19
Show Gist options
  • Save sqlchow/268b53569f32f5cea6aa to your computer and use it in GitHub Desktop.
Save sqlchow/268b53569f32f5cea6aa to your computer and use it in GitHub Desktop.
Get SQL Server instances installed on a local machine
function Get-SQLInstanceList
{
<#
.SYNOPSIS
Get SQL Server instances installed on a local machine
.DESCRIPTION
The script looks in the registry path
HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\
and will check which SQL*Server instances are installed and for each
installed instance it returns the Instance name, Instance Id and path to sqlservr.exe.
.LINK
https://sqlchow.wordpress.com/2013/02/13/t-sql-tuesday-39-getting-installed-instances-querying-logs/
.LINK
.NOTES
#>
try
{
$msSqlKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\'
$instNameKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\'
$sqlInstLst = @()
$instNames = (Get-Item -Path $instNameKey).GetValueNames()
foreach($inst in $instNames)
{
$instIDKey = $instNameKey
$instID = (Get-ItemProperty -Path $instIDKey -Name $inst).$inst
$sqlBin = (Get-ItemProperty -Path ($msSqlKey + $instID + '\Setup') -Name SQLBinRoot).SQLBinRoot
#HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.D11\Setup
$objInstDtls = New-Object System.Object|
Add-Member NoteProperty InstName $inst -PassThru|
Add-Member NoteProperty InstID $instID -PassThru |
Add-Member NoteProperty SQLBin $sqlBin -PassThru
$sqlInstLst += $objInstDtls
}
return $sqlInstLst
}
catch
{
$(throw "Unable to get SQL Server instances on: $($env:COMPUTERNAME)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment