2

I am trying to run this code in a windows batch (.bat) file

@echo off

echo Adding New User - LogMeInRemoteUser
net user | find /i "LogMeInRemoteUser" || Net user LogMeInRemoteUser password /add /fullname:"LogMeInRemoteUser"

pause

echo Adding User to Administrators Group
NET LOCALGROUP Administrators "LogMeInRemoteUser" /ADD

pause

echo Creating Registry Keys to remove the new user from the login page
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon\SpecialAccounts\UserList" /v "LogMeInRemoteUser" /t REG_DWORD /d 0

pause

echo Finished

if i run the file normally, i get an Access Denied error so I try to run as Administrator but the cmd windows opens and instantly closes, what have i done wrong?

  • Do the pause prompts appears? if not, have you called the bat file `net` or `reg` or `find` ? Try calling it `mybatch.bat` as a test. – foxidrive Mar 06 '14 at 03:23
  • The issue is related to incorrect handling special characters, see https://superuser.com/questions/1503300/why-can-we-not-run-as-administrator-a-batch-file-with-in-the-path – Arjen Jan 26 '23 at 16:04

1 Answers1

2

When you run as administrator the current directory is changed under you. To prove that (and fix it) enter these 3 lines under your @echo off

echo(%cd%
pushd %~dp0
echo(%cd%

You can remove both of the echo( statements after you see what is happening.

RGuggisberg
  • 4,630
  • 2
  • 18
  • 27