SQL Server - Get The Computer Name On Which The SQL Server Instance Is Running

In this blog, we will simply see how to get the computer name on which the SQL Server instance is running, using "SERVERPROPERTY".

What's the "SERVERPROPERTY"?

"SERVERPROPERTY" is a System-defined Function used to return the SQL Server instance information.

"SERVERPROPERTY" Syntax

  1. SERVERPROPERTY ('propertyname')

MACHINENAME

Use MachineName property to get the computer name on which the SQL Server instance is running.

Note
For a cluster, it returns the virtual server name.

Example

  1. Select SERVERPROPERTY('MachineName') as 'MachineName'.

Output

Get the computer name on which the SQL server instance is running using PowerShell?

You can use Windows PowerShell to invoke SQL command on a reachable server within the network using Invoke-Sqlcmd cmdlet as the following:
  • Open Windows PowerShell as Administrator
  • Type the Invoke-Sqlcmd with the below parameters.

    • -query: the SQL query that you need to run on the remote server.
    • -ServerInstance: the SQL server instance name.
    • -Username: the username that has sufficient permission to access and execute SQL query on the remote server.
    • -Password: the password of the elevated user.
  1. PS SQLSERVER:\> Invoke-Sqlcmd -query "select SERVERPROPERTY('MachineName') as 'MachineName'" -ServerInstance "epm\epmdb" -Username sa -Password *****

Applies To

  • SQL Server 2008.
  • SQL Server 2012.
  • SQL Server 2014.
  • SQL Server 2016.
  • SQL Server 2017.

Reference

See Also

Ebook Download
View all
Learn
View all