Skip to content

Instantly share code, notes, and snippets.

@robdmoore
Last active September 23, 2022 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save robdmoore/c22a9d305a0c16bf21b2 to your computer and use it in GitHub Desktop.
Save robdmoore/c22a9d305a0c16bf21b2 to your computer and use it in GitHub Desktop.
Automated install of SQL Express from commandline
# If you want to give owner access to the whole server to a particular user account this is how
exec sp_addrolemember 'db_owner', 'NT AUTHORITY\NETWORK SERVICE';
GO
# If you want to idempotently ensure a particular database with name DatabaseName exists this is how
IF NOT db_id('DatabaseName') IS NOT NULL BEGIN
PRINT 'Creating database...'
CREATE DATABASE [DatabaseName]
PRINT 'Created database.'
END
GO
function Install-SQLServerExpress2014AndManagementStudio() {
if (-not (Test-Path "C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS")) {
if (-not (Test-Path "C:\tempsql\SQLEXPRWT_x64_ENU.exe")) {
Write-Output "Downloading SQL Server 2014 Express: this may take a while`r`n"
mkdir c:\tempsql -Force
(New-Object Net.WebClient).DownloadFile('http://download.microsoft.com/download/E/A/E/EAE6F7FC-767A-4038-A954-49B8B05D04EB/ExpressAndTools%2064BIT/SQLEXPRWT_x64_ENU.exe','C:\tempsql\SQLEXPRWT_x64_ENU.exe')
}
Write-Output "Unpacking SQL Server 2014 Express`r`n"
C:\tempsql\SQLEXPRWT_x64_ENU.exe /x:"c:\tempsql\SQLEXPRWT_x64_ENU" /u | Out-Null
Write-Output "Adding .NET 3.5`r`n"
Import-Module Servermanager
Add-WindowsFeature NET-Framework-Core
Write-Output "Installing SQL Server 2014 Express: this may take a while`r`n"
C:\tempsql\SQLEXPRWT_x64_ENU\setup.exe /QUIETSIMPLE /ACTION=install /FEATURES=SQL,Tools /IAcceptSQLServerLicenseTerms /INSTANCENAME="SQLExpress"
$env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine")
Remove-Item -Recurse -Force c:\tempsql
} else {
Write-Output "SQL Server 2014 Express already installed`r`n"
}
}
Import-Module (Join-Path $PSScriptRoot sqlexpress.psm1) -Force
Install-SQLServerExpress2014AndManagementStudio
& sqlcmd -S ".\SQLEXPRESS" -E -i "$PsScriptRoot\setup.sql"
@robdmoore
Copy link
Author

Alternatively, you can use this which doesn't also install management studio (and the download drops from over 800MB to just under 200MB so the install is a lot faster):

function Install-SQLServerExpress2014() {
    if (-not (Test-Path "C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS")) {
        if (-not (Test-Path "C:\tempsql\SQLEXPR_x64_ENU.exe")) {
            Write-Output "Downloading SQL Server 2014 Express: this may take a while`r`n"
            mkdir c:\tempsql -Force
            (New-Object Net.WebClient).DownloadFile('http://care.dlservice.microsoft.com/dl/download/E/A/E/EAE6F7FC-767A-4038-A954-49B8B05D04EB/Express%2064BIT/SQLEXPR_x64_ENU.exe', 'c:\tempsql\SQLEXPR_x64_ENU.exe')
        }
        Write-Output "Unpacking SQL Server 2014 Express`r`n"
        C:\tempsql\SQLEXPR_x64_ENU.exe /x:"c:\tempsql\SQLEXPR_x64_ENU" /u | Out-Null
        Write-Output "Adding .NET 3.5`r`n"
        Import-Module Servermanager
        Add-WindowsFeature NET-Framework-Core
        Write-Output "Installing SQL Server 2014 Express: this may take a while`r`n"
        C:\tempsql\SQLEXPR_x64_ENU\setup.exe /QUIETSIMPLE /ACTION=install /FEATURES=SQL /IAcceptSQLServerLicenseTerms /INSTANCENAME="SQLExpress"
        $env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine")
        Remove-Item -Recurse -Force c:\tempsql
    } else {
        Write-Output "SQL Server 2014 Express already installed`r`n"
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment