Get-DBInstances.ps1
19 Sep 2025Description
Purpose
Retrieves the SQL Server instances available on a specified computer.
Detailed Description
The Get-DBInstances function retrieves the SQL Server instances available on a specified computer. It first checks if the computer is part of a failover cluster and if so, it uses the FailoverClusters module to get the SQL Server instances from the cluster. If the computer is not part of a cluster, it retrieves the instances from the local registry.
Usage
Example 1
Get-DBInstances -ComputerName "Server01"
Retrieves the SQL Server instances available on the computer named “Server01”.
Notes
Author: Your Name Date: Current Date
Script
<#
.SYNOPSIS
Retrieves the SQL Server instances available on a specified computer.
.DESCRIPTION
The Get-DBInstances function retrieves the SQL Server instances available on a specified computer. It first checks if the computer is part of a failover cluster and if so, it uses the FailoverClusters module to get the SQL Server instances from the cluster. If the computer is not part of a cluster, it retrieves the instances from the local registry.
.PARAMETER ComputerName
The name of the computer on which to retrieve the SQL Server instances.
.EXAMPLE
Get-DBInstances -ComputerName "Server01"
Retrieves the SQL Server instances available on the computer named "Server01".
.NOTES
Author: Your Name
Date: Current Date
#>
function Get-DBInstances {
Param(
[Parameter(Mandatory = $true)]
$ComputerName
)
if ($null -ne (Get-CimInstance -ClassName MSCluster_ResourceGroup -ComputerName $ComputerName -Namespace root\mscluster -ErrorAction SilentlyContinue)) {
Import-Module FailoverClusters
Get-ClusterResource -Cluster $ComputerName -ErrorAction SilentlyContinue |
Where-Object { $_.ResourceType -like "SQL Server" } |
Get-ClusterParameter VirtualServerName, InstanceName | group-object ClusterObject |
Select-Object @{Name = "SQLInstance"; Expression = { [string]::join("\", ($_.Group | Select-Object -ExpandProperty Value)) } }
}
else {
$SQLInstances = Invoke-Command -ComputerName $ComputerName {
(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
}
foreach ($sql in $SQLInstances) {
[PSCustomObject]@{
ServerName = $sql.PSComputerName
InstanceName = $sql
}
}
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.