0

I successfully added my mysql path to System Variables in Windows variable Path. If I run cmd and then mysql.exe. It will execute the program without problems.

I want to run mysql.exe from PowerShell

$mysql = "mysql.exe"
$params = "-C","-B","-h$server","-P$port","-u$user","-p$password",$db,"-e$query"
&  $mysql @params

I tried ./mysql.exe and mysql.exe but it's not working.

I get the error: The term 'mysql.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.

Thanks in Advance for your Help.

2
  • If dot sourcing from a working directory is giving you that error, you'll need to either dot source using the full path, add the mysql.exe to your environmental path, or use the "Start-Process" cmdlet (with the full path). Commented Oct 25, 2017 at 16:07
  • .\command or ./command is not dot-sourcing. Dot-sourcing is when there is a space after the dot. Run help about_Scopes for more details and read the section titled "Using Dot Source Notation with Scopes." Commented Oct 25, 2017 at 16:48

1 Answer 1

0

Remember that PowerShell is a shell that can run commands: You put the command at the beginning of the line, and you put the arguments afterward. If any of the arguments come from variables, just put them there. This is all you need to do:

mysql.exe -C -B "-h$server" "-P$port" "-u$user" "-p$password" $db "-e$query"

For parameters that have their argument "pasted" directly to the parameter (like -P<port>), quote the parameter so PowerShell knows to do the variable expansion within the string.

Of course in this example we are assuming that mysql.exe is in the Path. If it's not and you need to specify the full path and filename to mysql.exe, just put the full path and filename of mysql.exe:

& "C:\Program Files\MySQL\bin\mysql.exe" ...

(That path is made up.) The quotes (") are required since the path contains white space, and the & tells PowerShell to execute the quoted string as a command (rather than simply interpreting it as a string).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it also worked with $params = "-C","-B","-h$server","-P$port","-u$user","-p$password",$db,"-e$query" & mysql @params @Bill_Stewart
The commas are not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.