Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

Powershell script to find SQL Server Instances on a Server

25 March,2019 by Tom Collins

Question: How can I find the SQL Server Instances on a server using Powershell ? Either the local server or remote SQL Servers.

The script should be based on  reporting the name after the MSSQL$   . For example , if the SQL Server Instance is called MSSQL$MYSERVER1  , than only MYSERVER1 should be returned

 

Answer: You can use this Powershell script as a basis to create a script . Place the  code in a powershell script file , and then execute 

.\Find_SQL_Server_Instances_on_server.ps1 -server MYSERVER1

 

param (
  
     [Parameter(Mandatory=$true)]
     [string]$server
         
)

$SERVER=$server

function getSQLInstanceOnServer ([string]$SERVER) {
    $services = Get-Service -Computer $SERVER
    $services = $services | ? DisplayName -like "SQL Server (*)"
        try {
        $instances = $services.Name | ForEach-Object {($_).Replace("MSSQL`$","")}
    }catch{
        # if no instances are found return 
        return -1
    }
    return $instances
}

getSQLInstanceOnServer $SERVER

 

You can also read these posts for some extra ways to enhance your Powershell scripts or as alternatives to the above . 

Powershell script - Get the SQL Server Instance status with Get-Service

List SQL Server Instances using Powershell and Get-ItemProperty

List SQL Server Instances with Powershell remote registry search


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Powershell script to find SQL Server Instances on a Server


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer