Windows – net use command works in command prompt, but not in bat file

batchcommand linenet-usewindowswindows-server-2008-r2

  • It's a small laboratory network that connects to another network via an FTP server.

  • FTP server is managed by the other network.

  • This lab network is basically 2 clients and one server with several other IP devices connected.

  • This lab server has Active Directory installed on it.

  • We have an account on each network. The names of these acct's are the same for each network, and they have the same password.

  • Password does have a % sign and a $ sign in it.

  • Trying to map drive at startup on the lab server to point to the FTP server.

  • Lab server is logged in with local admin acct.

  • Command I'm using to map drive:
    net use j: \x.x.x.x\LabTransfer /user:\username "password"

This command will work when typed in a command prompt;
BUT fails to run when ran in a .bat file or .cmd file

Error message: "System error 86 has occured. The specified password is not correct.

Why would the net use command work at a command prompt and not in the .bat file?

Best Answer

The percent sign (%) is a special case. On the command line, it does not need quoting or escaping unless two of them are used to indicate a variable, such as %OS%. But in a batch file, you have to use a double percent sign (%%) to yield a single percent sign (%). Enclosing the percent sign in quotation marks or preceding it with caret does not work.

[…]

  • echo The ratio was 47%
    • If run from a batch, the percent sign is ignored.
  • echo The ratio was 47%%
    • If run from a batch, the percent sign is output once.

(source)