1

To execute the command:

icacls "C:\Program Files\MyDir" /grant myusername:(OI)(CI)(F)

I have not enough rights. Can I make Windows to ask me to get the rights for example via admin account like the images below ? And how ? The script will be executed by WinRar automatically so no manual action must be needed before Windows would prompt for password etc. So I would like to achieve following behaviour:

  1. Run part of script
  2. Need rights -> prompt for password etc.
    1. Got rights, continue with execution
    2. No rights, break operation

Edit: I found this. And it solved my problem.

Like this:

enter image description here

or this

enter image description here

Community
  • 1
  • 1
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • possible duplicate of [How can I auto-elevate my batch file, so that it requests from UAC admin rights if required?](http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-admin-rights) – Harry Johnston Aug 07 '14 at 21:29

3 Answers3

1

Put this in for step 2:

NET FILE 1>NUL 2>NUL
if not '%errorlevel%' == '0' powershell "saps -filepath %0 -verb runas" >nul 2>&1 &exit/b
cd /d "%~dp0"
Ir Relevant
  • 963
  • 6
  • 12
  • This goes in the right direction but I couldn't get this running. See edit in my question. There is an answer that solves it. Dunno what is better but the other worked for me. – Bitterblue Aug 07 '14 at 09:11
  • Why not? What happens? – Ir Relevant Aug 07 '14 at 17:40
  • Nothing. Maybe you edit it so that it prints something like user name. It wouldn't ask me for admin permission or anything. I'm not very familiar with scripting. Please don't blame me. – Bitterblue Aug 08 '14 at 06:25
  • If the script has a space in the path then change %0 to %~s0 (short name is probably enabled). Or maybe you don't have powershell? – Ir Relevant Aug 08 '14 at 13:41
0

Start the CMD windows as an administrator using RUN AS: Windows key - in search box, enter CMD, press Ctrl+Shift+Enter (not just Enter) to Run As Administrator. You should be prompted for administrator credentials.

kaufman
  • 114
  • 1
  • 1
0

Another solution is to create a shortcut to the batch script and tick the checkbox for "Run as administrator" (under "Advanced").

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158