Check SQL Server Version Using PowerShell

  1. Use the sqlcmd Utility to Check the SQL Server Version in PowerShell
  2. Use Invoke-Sqlcmd Cmdlet to Check the SQL Server Version Using PowerShell

Checking the version of a program is one of the common operations you can do in PowerShell. If you are using the Microsoft SQL server for managing relational databases, it is important to know which version is installed on your computer.

This tutorial will introduce multiple methods to check the SQL server version with PowerShell.

Use the sqlcmd Utility to Check the SQL Server Version in PowerShell

The sqlcmd is a command-line utility that allows you to run interactive Transact-SQL statements and scripts. It helps to automate Transact-SQL scripting tasks.

The following command prints the SQL server version in PowerShell. The DelftStack is the server name, and the SQLEXPRESS is the instance name of our SQL server.

sqlcmd -S DelftStack\SQLEXPRESS -Q "SELECT @@VERSION"

The -S option specifies the server, and the -Q option runs the command-line query and exits.

Output:

Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64)
    Sep 24 2019 13:48:23
    Copyright (C) 2019 Microsoft Corporation
    Express Edition (64-bit) on Windows 10 Home Single Language 10.0 <X64> (Build 22000: )

(1 rows affected)

The above output shows that the installed version of the SQL server is 15.0.2000.5.

Use Invoke-Sqlcmd Cmdlet to Check the SQL Server Version Using PowerShell

The Invoke-Sqlcmd cmdlet executes the scripts and commands supported by the SQL Server SQLCMD utility. It accepts Transact-SQL statements and commands such as GO and QUIT.

The following command gets the SQL server version in PowerShell.

Invoke-Sqlcmd -query "SELECT @@VERSION" -ServerInstance "DELFT-PC\SQLEXPRESS"

Output:

Column1
-------
Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) ...

If the Invoke-Sqlcmd cmdlet is not recognized, you must install it using the below command.

Install-Module SqlServer

Then run this command to import the module.

Import-Module SqlServer

For more information on Invoke-Sqlcmd, read this documentation.

Related Article - PowerShell SQL

  • Running SQL Queries in PowerShell