0

I have a .CMD file that I run by right-clicking on a file in Windows Explorer; the file that I click on gets passed as the parameter to the .CMD file. The .CMD file does some processing and then copies a file from one folder to another. It worked fine on Windows XP; on Windows 8, when the "copy" command runs, it returns the error message "Access is denied".

If I copy the file using Windows Explorer, a dialog box pops up with the message "You'll need to provide administrator permission to copy to this folder". I click on "Continue", and the file gets copied.

I don't know the details of UAC, but I know that I don't want to change permissions to allow the file to be copied without intervention. That is, I don't want the whole .CMD file to run as administrator. I want to right-click the input file, run the .CMD file, and have the dialog appear when the "copy" command tries to run; I'll then click on "Continue" to allow only the copy command to run with administrator privilege. (If there were another place in the .CMD file where a file were copied, or any other operation that required administrator privilege, I would want the dialog to appear again, to ask if it's ok to run that operation as administrator).

Can this be done?

2 Answers 2

2

I'm afraid there's no simple solution to what you're trying to do.

  • cmd is not very interactive, and scripts are intended to be run without human attention.
  • another limitation lies in the way UAC works: a process can be elevated only when it starts; after a process is started, it either has administrator privileges or not.
    So in your case, for every command that could potentially lead to UAC prompt, you have to code both ways: where the script has permission to copy without elevation, and with the elevation.

You can't program elevation with cmd unless you have an additional tool which handle elevation for you and then proceed with the command that failed with regular permissions.


If you were OK with the way it worked on Windows XP, the most simple solution is to run your script with administrator privileges.

If the purpose of your script is to copy one single file and that's all the script does, creating utility that plays nice with UAC seems like overkill.


You can create shortcut, .lnk file, for your script. Then in shortcut properties, on Shortcut tab, click Advanced button and check Run as administrator. Click OK twice to save changes.

When you click such shortcut, you'll see UAC prompt for Windows Command Processor (because it executes .cmd files).

4
  • I've edited the question yet again, trying to be clearer, in case anyone can help; but @Alexey Ivanov: I think you've made it clear that what I want to do can't be done. I'll try the shortcut (.lnk file). Thanks.
    – user184411
    Commented Sep 22, 2013 at 15:23
  • The shortcut didn't work; I'll try sudowin.
    – user184411
    Commented Sep 22, 2013 at 17:58
  • @user184411 How didn't shortcut work? Commented Sep 23, 2013 at 5:06
  • @ Alexey Ivanov: I'll tell you when I get back to my computer, early in October.
    – user184411
    Commented Sep 24, 2013 at 0:22
2

You need to open the command prompt as an administrator.

  1. Open the task manager, either by opening the run dialog (press Win + R together) and typing taskmgr, or by pressing Ctrl + Shift + Esc.

  2. Click File --> New Task.

  3. Check the box that says Create this task with administrative privileges. If you don't see this, it means that your user account is a standard account, not an administrator account. Since you can click through the UAC prompt, I'm assuming you're an administrator.

  4. Type cmd and click OK. You should be able to run your copy command from this command prompt without receiving any permission errors.

Alternatively, you can press Win + X to bring up the Power User Menu, and select

power user menu

and select Command Prompt (Admin) from there (or press A)

2
  • I've edited the question, and added some more detail, to show why this isn't the answer I'm looking for.
    – user184411
    Commented Sep 19, 2013 at 6:25
  • 1
    @user184411 It sounds like you need a batch file. Place your copy command in a batch file, then run that as administrator. Commented Sep 19, 2013 at 11:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .