I am trying to create a batch file that needs to be passed a path and has operations in it that requires elevation (I use mklink in it).

Here is how to recreate the problem.

  1. Make a .bat file that just contains pause
  2. Make a shortcut to that batch file.
  3. Create 2 folders, C:\ThisWillWork and C:\This Will Fail
  4. Try dragging each folder on to the shortcut. Both should work.
  5. Change the shortcut settings and check in the advanced options options that it requires administrator privileges.
  6. Try dragging each folder again, C:\ThisWillWork will behave the same but C:\This Will Fail will flash the command prompt and immedatly close itself.

This is being done on Windows 7 64-bit.

What can I do to accomplish my goal? Is there a way to allow me to use mklink without elevation? Do I need to switch from using windows batch files to something else (powershell maybe)? What else can I use to be able to write a script to delete a folder then recreate it with as a directory symlink to the folder I dropped on it?

  • MKLINK (anything that makes a symbolic link actually) requires Admin access (turn off UAC perhaps?) as it makes a system-wide change. That whole path with a space thing is kind of weird for sure! If the helps any, it does the same thing on mine too, so it's not just you. ;) – Ƭᴇcʜιᴇ007 Nov 11 '11 at 19:36
  • @techie: SeCreateSymbolicLinkPrivilege, not full Admin access. As for "system-wide changes", it's not much more "system-wide" than creating a regular file. – grawity Nov 11 '11 at 20:37
  • @grawity True enough, I was thinking that it would have to be registered beyond the usual file system. – Ƭᴇcʜιᴇ007 Nov 11 '11 at 20:46
  • @grawity SeCreateSymbolicLinkPrivilege was the kind of thing I was looking for, would you mind writing up the steps on how to set that for a normal user and post that as a answer too. it would be good for other people who find this page. – Scott Chamberlain Nov 11 '11 at 20:48
  • 1
    @Scott: They wouldn't find this page because it discusses a somewhat different question (elevation and whitespace in batch file arguments). This answer should help. – grawity Nov 11 '11 at 21:44

Try creating a shortcut as

%Windir%\System32\cmd.exe /C C:\BIN\batchfile.bat 

If that doesn't solve it on its own try the following.

To resolve this problem, change the Start in value in the Command Prompt Properties dialog box or in the Notepad Properties dialog box.

To do so, follow these steps:
Click Start, point to All Programs, and then point to Accessories.
Right-click Command Prompt or Notepad, and then click Properties.
In the Start in box, change the value from %HOMEDRIVE%%HOMEPATH% to a system-wide value such as %WINDIR%.
Click OK.

See http://support.microsoft.com/kb/832434 for full details.

If you to see more of what is going on you with your original method, you can open a command prompt as admin and type

%windir%\System32\cmd.exe /C "C:\bin\batchfile.bat" "C:\THIS WILL FAIL" 

This will give an error without loading the batchfile as it fails some command line parsing.

  • The KB may or may not be relevant, but I had tried both and wasn't sure if both are required. If you run cmd /? you will see lots of options relating to how it parses the command line, especially whether or not removes quote characters. The batch file is not relevant as it does not get as far as opening it. In fact cmd /C "X" "C:A B" does not parse, but cmd /C X "C:A B" does and so does cmd /C "X" "CA B". – sgmoore Nov 11 '11 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.