Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Connecting to LocalDB


In this recipe, we will connect to the LocalDB database.

Getting ready

LocalDB is a separate installation from your usual instance. LocalDB needs to be installed before you can follow this recipe.

You can download LocalDB with SQL Server Express from MSDN downloads at https://www.microsoft.com/en-ca/download/details.aspx?id=42299.

How to do it...

Let's take a look at the steps to connect to LocalDB:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module and create a new SMO Server object as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run it:

    $instanceName = "(localdb)\MSSQLLocalDB"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
    
    #list all databases in localdb
    $server.Databases

How it works...

LocalDB is a flavor of SQL Server Express that is geared toward developers. You can think of it as a stripped-down version of SQL Server Express—it is a simple installation...