Microsoft SQL Server logo

Update SQL Server Management Studio (SSMS) automatically

The fire-and-forget method to update SQL Server Management Studio (SSMS) automatically by uninstalling any installed version and downloading & installing the newest available version from Microsoft.

Home » Update SQL Server Management Studio (SSMS) automatically

The fire-and-forget method to update SQL Server Management Studio (SSMS) automatically. The script below starts with uninstalling any installed SSMS version. Once that is succeeded, it downloads & installs the latest available version from Microsoft.

This script is ideal for in your automated (unattended) deployments of SQL Servers or management servers needing this tool.

<#
Updates SQL Server Management Studio (SSMS) unattended
#>

Function Remove-SqlServerManagementStudio {
  <#
  Removes any installed SQL Server Management Studio (SSMS) version
  <#
  $installedapps = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*
  $installedapps | Where-Object { $_.Displayname -like "Microsoft SQL Server Management Studio*" } | Select-Object DisplayName,VersionMajor
  $app = $installedapps | Where-Object { $_.Displayname -like "Microsoft SQL Server Management Studio*" }
  if(!([string]::IsNullOrEmpty($app))) {
    $executable = $app.UninstallString.Split('"')[1]
    $arguments = @("/uninstall", "/quiet")
    if(test-Path "C:\Program Files (x86)\Microsoft SQL Server Management Studio $($app.VersionMajor)\Common7\IDE\Microsoft.AnalysisServices.Deployment.exe") {
      Remove-Item -Force "C:\Program Files (x86)\Microsoft SQL Server Management Studio $($app.VersionMajor)\Common7\IDE\Microsoft.AnalysisServices.Deployment.exe"
    }
    $process = Start-Process -FilePath $executable -ArgumentList $arguments -PassThru
    $handle = $process.Handle
    $process.WaitForExit()
    if($process.ExitCode -ne 0) {
      Write-Warning "$_ exited with status code $($process.ExitCode)"
      return $false
    } else {
      return $true
    }
  }
}

Function Install-SqlServerManagementStudio {
  <#
  Download and install the latest available version at Microsoft
  #>
  $ProgressPreference = 'SilentlyContinue'
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  $url = "https://aka.ms/ssmsfullsetup"
  $result = Invoke-WebRequest -Method GET -Uri $url -Headers $headers -UseBasicParsing
  $tempFolder = $env:temp

  $contentDisposition = $result.Headers.'Content-Disposition'
  $fileName = $contentDisposition.Split('=')[1].Split(';')[0]
  $path = Join-Path $tempFolder $fileName

  $file = [System.IO.FileStream]::new($path, [System.IO.FileMode]::Create)
  $file.write($result.Content, 0, $result.RawContentLength)
  $file.close()

  # GC, clean up memory
  Remove-Variable result

  $media_path = $file.name
  $params = @("/Install", "/Quiet")

  $process = Start-Process -FilePath $media_path -ArgumentList $params -PassThru
  $handle = $process.Handle
  $process.WaitForExit()
  if ($process.ExitCode -ne 0) {
    Write-Warning "$_ exited with status code $($process.ExitCode)"
    return $false
  } else {
    return $true
  }
}

if(Remove-SqlServerManagementStudio) {
    write-output "Old SQL Server Management Studio version successfully removed."
  if(Install-SqlServerManagementStudio) {
    write-output "New SQL Server Management Studio version successfully installed."
  } else {
    Write-Output "Oops, installing new version failed."
  }
} else {
  Write-Output "Oops, removing old version failed."
}

Next to this script you can also use command-line parameters to install SQL Server Management Studio.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Share via
Copy link