Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:10:08+00:00 2026-06-04T02:10:08+00:00

Im writing a script in powershell which is supposed to connect to a MySQL

  • 0

Im writing a script in powershell which is supposed to connect to a MySQL server, create a new database, and then run query commands against that database. I have a premade .sql file with all of the commands to be executed, however there seems to be a problem with my code where it never actually executes the .sql file. Any help would be appreciated. My code is below.

[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")

#Creates a connection to the server
$connStr ="server=mySql;Persist Security Info=false;user id=" + "username" + ";pwd=" + "pass" + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)

#Open connection
$conn.Open()

#Drops database if it currently exists
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection  = $conn
$cmd.CommandText = "DROP DATABASE IF EXISTS " + $dbname
$cmd.ExecuteNonQuery()

#Create the new database
$cmd.CommandText = 'CREATE SCHEMA `' + $dbname + '`'
$cmd.ExecuteNonQuery()

#Load the sql file
$sql = (Get-Content C:\"path to sql file") -join "'r'n"
$cmd.CommandText = $sql

#Close connection
$conn.Close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:10:09+00:00Added an answer on June 4, 2026 at 2:10 am

    Aren’t you missing a $cmd.ExecuteNonQuery() call ? And it looks like you’re joining the file lines with the literal “‘r’n”. You need to use the back tick character instead (e.g
    “`r`n”). I would use the Out-String cmdlet to convert the file content to one string:

    #Load the sql file
    $sql = Get-Content C:\"path to sql file" | Out-String
    $cmd.CommandText = $sql
    $cmd.ExecuteNonQuery()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a PowerShell script that uses the SharePoint 2010 New-SPWeb cmdlet to create
I'm writing a powershell script to deploy a number of reports to SQL Server
I'm writing a PowerShell script in which I need to get a list of
I'm writing a PowerShell script that will execute commands on a remote host using
I'm writing a powershell v2 script that I'd like to run against a remote
I'm writing an application that can run PowerShell scripts which I'd like to be
I just finished writing a script with PowerShell and I need it to run
I'm writing some PowerShell scripts to work with our source control software (which is
I am writing a PowerShell script that will back up several folders from my
I'm writing a powershell script for deployment. I need to copy changed files from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.