Cannot find type [microsoft.sqlserver.management.smo.server]

I was trying to retrieve the SQL edition using a PowerShell script recently. After executing that query, I got this error. In this article, I will discuss the cause and the solution to this error.

Cannot find type [microsoft.sqlserver.management.smo.server]

I was executing the below query.

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LAPTOP-KK75M6V0\LOCALDB#75161C73"
$s.Edition

After executing the above query, I got the error as shown below.

New-Object : Cannot find type [Microsoft.SqlServer.Management.Smo.Server]: verify that the assembly 
containing this type is loaded.
At line:2 char:6
+ $s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LAPTOP ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You can see the same error shown in the screenshot below.

Cannot find type [microsoft.sqlserver.management.smo.server]

Solution

To fix the error, follow the below steps.

1. Open Windows PowerShell ISE with Run as administrator.

Powershell Cannot Find Type microsoft.sqlserver

2. Now, execute the below PowerShell command to install the SqlServer module.

Install-Module -Name SqlServer

After executing the above command, I got the expected output, as shown below.

Powershell Cannot Find Type microsoft.sqlserver.management.smo.server

3. Now, execute the Powershell script below with the import SQL server statement, as shown below.

Import-Module -Name SqlServer
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LAPTOP-KK75M6V0\LOCALDB#75161C73"
$s.Edition

After executing the above command, I got the expected output, as shown below, with no error.

Cannot Find Type microsoft.sqlserver.management.smo.server

Video Tutorial

Conclusion

This article discussed how I fixed the error Cannot Find Type microsoft.sqlserver.management.smo.server. Now, you can select the above steps to fix this error.

You may also like following the articles below.

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.