Skip to content

Instantly share code, notes, and snippets.

@jdstaerk
Created March 21, 2017 08:35
Show Gist options
  • Select an option

  • Save jdstaerk/5a0a369a05d2612eb38e93d9bba67456 to your computer and use it in GitHub Desktop.

Select an option

Save jdstaerk/5a0a369a05d2612eb38e93d9bba67456 to your computer and use it in GitHub Desktop.
# Use host\instance,port in general(port is only needed if you connect via TCP; Not needed for localhost)
$DataSource = "localhost\SQLEXPRESS"
$Username = "MyUser"
$Password = "MyPassword"
# Create a new server object
$Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $DataSource
# Use LoginSecure = $true if you are using the Windows Authentification --> Integrated_Security
# If you do so, you don't need the ConnectionContext.Login or .Password
$Server.ConnectionContext.LoginSecure = $false
$Server.ConnectionContext.Login = $Username
$Server.ConnectionContext.Password = $Password
# Create a new database
$NewServerObj = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Database -ArgumentList $Server , "MyDatabaseName"
$NewServerObj.Create()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment