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 5966271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:46:27+00:00 2026-05-22T19:46:27+00:00

Here is the command I have been using to back up one of my

  • 0

Here is the command I have been using to back up one of my MySQL databases:

mysqldump.exe --user=myuser --password=mypassword --databases --opt MyDatabase > "C:\MyDatabase.sql"

I’d like to use this command in a PowerShell script. However, if an error occurs, I don’t want it to be outputted to the console. Instead, I would like it captured in a variable. I’ve been trying various methods of doing this, but all have failed. Any assistance would be appreciated. Thanks!

  • 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-05-22T19:46:28+00:00Added an answer on May 22, 2026 at 7:46 pm

    Firstly, beware > if your database dump does not need to be in Unicode, but rather ASCII. You would instead use | out-file $filepath -enc ascii to make sure it writes in ASCII encoding. (My databases are in latin1. If I use Powershell’s > the dump file is twice as large as when dumped using > from normal console.)

    That said, I got it to dump to a file in ASCII format and put the errors in a variable in this way (forcing an error by trying to dump a nonexistent database, but mysqldump outputs a number of initializing lines of the dump before it throws the error):

    $err = (mysqldump -uroot -p --databases nonexistent | out-file C:\backup.sql -enc ascii) 2>&1
    

    This wraps the execution so that stdout is piped to out-file, but after that wrapping, stderr is redirected to stdout so that it can be assigned to variable $err.

    $err is 1 ErrorRecord or more. A single error has a TargetObject property containing the original stderr output. Here’s a demo of printing every error string to the console:

    if ($err -is [system.array]) {
        $err | % { write-host $_.TargetObject }
    } else {
        write-host $err.TargetObject
    }
    

    Alternately, you can ensure that $err is an array by slightly modifying the original dump statement by wrapping it an @() (good idea, Mike):

    $err = @((mysqldump -uroot -p --databases nonexistent | out-file C:\backup.sql -enc ascii) 2>&1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Doug Hellmann's script to back up my repo for 2
Here is a command line script for a dictionary lookup using Wordnet: #!/bin/bash #
Problem: to get the command working here. My domain is http://cs.edu.com/user/share_dir , but I
I'm trying to generate a RDoc using the XML format. Here's the command that
I have been using Unity for quite a while but I have always used
I have been using Windows 7 for a while but have not had to
We have been using SimplePie on our Magento install to pull our blog posts
I have been using Jasmine to write BDD tests for a web app. I
I have been using python with regex to clean up a text file. I
I have been successful with extracting text from a PDF file using GhostScript along

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.