top of page

Get the port of a SQL instance in Powershell

The default port for an MS SQL server is 1433 but you will often find that, for security reasons, the port has been changed.


Here is a small PowerShell script allowing you to return the instances as well as the used ports of an MS SQL server.


$server = Read-Host -Prompt 'Input your servername'

$namespace = gwmi -computername $server -Namespace "root\microsoft\sqlserver" -Class "__Namespace" -Filter "name like 'ComputerManagement%'" | sort desc | select -ExpandProperty name -First 1

Get-WmiObject -computername $server -Namespace "root\microsoft\SqlServer\$namespace" -Class ServerNetworkProtocolProperty | select instancename,propertystrval,PropertyName,IPAddressName,ProtocolName | where{$_.IPAddressName -eq 'IPAll' -and $_.propertystrval -ne ''} | ft -autosize

When you run this script, it will ask you to enter the server for which you want to find the instances and ports and you will obtain the following result:



Enjoy! 😉





1 view

Comments


bottom of page