Running the .bat batch file in administrator mode - windows

Iam trying to synch certain computers to a TimeServer within the network, hence i've written a simple batch script to do the task. I've put into our WDS server.
How do i run it in administrator mode.
If i open the cmd prompt as administrator it starts with "C:\Windows\System32\"
this is where i want to run so how do i achieve it.I dont want to schedule it nor i want to use RMB Click and select "Run as Administrator"
OS: Win 2012R2
My batch file contains.
reg add HKLM\SYSTEM\CurrentControlSet\services\w32time\Config /v MaxNegPhaseCorrection /t REG_DWORD /d 0xffffffff /f
reg add HKLM\SYSTEM\CurrentControlSet\services\w32time\Config /v MaxPosPhaseCorrection /t REG_DWORD /d 0xffffffff /f
w32tm /config /syncfromflags:manual /manualpeerlist:"132.186.XX.XX 132.186.127.XX 132.186.192.XX 132.186.XXX.XX" /largephaseoffset:120000
net stop w32time
net start w32time
w32tm /resync
Thanks in Advance

Create a shortcut to the batch file.
Right click on the shortcut, go to properties -> shortcut tab -> advanced.
Tick run as adminstrator

Add this to the start of your batch file:
runas.exe /savecred /user:#administratoraccount# "%windir%/System32/cmd.exe"
Replace #administratoraccount# with the username of an admin.
The first time you run it, it will ask for the password of that admin account. Enter it. Unless the password is removed from the windows credential locker or the password or username of that admin is changed or the username is disabled, whenever the batch file is run from then on, it will automatically run cmd.exe as administrator.

Related

How can i change values in Windows registy without admin permissions

i want to automate something, but it only works if i manually accept the uac window, so i tried to change the registry value with this commads:
c:\Windows\System32\cmd.exe /k c:\Windows\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
But it only works if i run the cmd as administrator. So again i need to accept the uac window to deactivate the uac windows...
Is there a good way to make this with a script ore something like that, or is there a bypass solution? (for automation puroses)
If you want to modify a registry hive without administrative privileges, you have to do it offline1.
To do so, you can boot into WinRE and then use regedit to modify your hives offline. You can also remove your hard disk and put it into another computer where you have administrative privileges and modify it there.
You can find an example here.
1 Offline in this case means, the hives are not mounted. Or in other words, the Windows installation is not running.

How to disable Fast Boot (Fast Startup, Hybrid Boot) only once by the next shutdown?

The problem is;
Our software (setup) detects that the computer (win10), it is running on, needs to be restarted due to file operations, other setups and so on. Then we are asking for restart now or later to continue.
If the user restarts the computer immediately or later, pending operations have been done, and all is ok. But if the user only does shutdown (from start menu) and later starts the computer up again (there is a Fast Boot) pending operations would not been done. It is clear, because of fast boot...
My question is; How can i programatically say windows to disable fast boot only once by the next shutdown? I don't want disable it completely and activate it after next startup. Is it possible?
Thanks to the comment of RbMm on the comments of the answer I did this script which disables the Hybrid boot and adds a command to the RunOnce key in the registry to reenable it on the next boot.
The batch needs to be run with administrator rights:
#ECHO OFF
%COMSPEC% /C reg add "hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /F
%COMSPEC% /C REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v DisableHiberbootOnce /t REG_SZ /d "%COMSPEC% /C reg add """hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Power""" /v HiberbootEnabled /t REG_DWORD /d 1 /F
As RbMm said the HLKM RunOnce key only it's executed if one administrator logins on the computer. To fix this I run on a GPO computer login script the command runonce.exe /explorer which runs the RunOnce scripts on the HKLM registry key without the need to log in as administrator.

Windows Batch file enable quick edit for gitbash

I managed to launch gitbash from a bat file, I want also to enable quick edit mode how to do that ? thanks.
cd "C:\Program Files (x86)\Git\bin\"
mode con: cols=160 lines=78
start sh.exe --login -i
You can set a registry key to enable QuickEdit mode on all console windows launched by the current user.
reg add "HKCU\Console" /v "QuickEdit" /t REG_DWORD /d 1
You should only need to run this command once, until you switch users or reinstall your OS.

How do I automatically launch a non-Surface application in Windows Embedded 8?

How do I automatically launch a non-Surface application in Windows Embedded 8?
I want to automatically launch a GUI application on startup in Windows Embedded 8, but I can't figure out how to do it. I've been reading Sean Liming's Professional's Guide to Windows Embedded 8 Standard, and in chapter 8 he describes how to modify the device experience. He names how there is a Windows 8 Application launcher, but for Surface apps only. He also describes a Shell Launcher module, but my app is not a shell. He also mentions a shell he wrote here, but reading through its documentation, it doesn't describe how to automatically launch a program within that shell.
As far as I can tell, he doesn't describe how to automatically launch a non-Surface application anywhere, and Google and Stack Exchange get me no results (it doesn't help that most results come back as merely Windows 8, and not Windows Embedded 8). Or am I mistaken? Is Shell Launcher sufficient to launch a non-shell app? Does the app launched become the "shell", in effect?
Side note: It's probably worth mentioning that the app I want to launch is a Java app. I will be including the module-based Java JRE installer as mentioned in the book, but if there are any other provisos to launching a Java app in WE8S, please comment.
Thanks for your time and feedback!
This works for both Windows Embedded Standard 7 and Windows Embedded 8 Standard:
I normally install Windows Embedded Standard with the standard shell. In WE8S this would be the Metro UI. Once installation and configuration is complete, and your application runs successfully, I use registry entries to modify the application launched on startup.
Custom user-specific shell
As an example, to launch VLC media player as the shell, and play media files in a folder on the d:\ e.g d:\media in a continuous loop, I use the below in a .bat file, running as administrator.
This must be run while logged into the user that will launch the custom shell
Create a new .bat file:
in Windows Explorer, Select "File" -> "New" -> "Text Document"
Rename "New Text Document.txt" to custom_shell.bat
Paste the below into the file
Save the file
Right click the file -> "Run as Administrator"
c:\Windows\system32\reg.exe DELETE "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /f
c:\Windows\system32\reg.exe ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /t REG_SZ /d explorer.exe
c:\Windows\system32\reg.exe ADD "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /t REG_SZ /d "c:\program files\vlc\vlc.exe -f --loop ""d:\media"""
c:\Windows\system32\reg.exe DELETE "HKLM\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\system.ini\boot" /v Shell /f
c:\Windows\system32\reg.exe ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\system.ini\boot" /v Shell /t REG_SZ /d "USR:Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
pause
This modifies the shell for the !Current User! (i.e. the logged on user) to launch VLC.exe on logon. (I use reg.exe because some slimmed down installations might not have regedit.exe included)
This means that you can still access the full user interface when logging on as the administrator user (via safe mode if Administrator profile is normally disabled), since the shell for all other users is still the explorer shell.
Launching the explorer shell from within the custom shell
You can still launch the explorer shell with the metro UI when the user with the custom shell is logged on. To launch the explorer shell from the user running the VLC (custom) shell:
Start the task manager (CTRL+SHIFT+ESC)
Click on the "Advanced" button at the bottom of the task manager
Select "File" -> "New Task (Run...)" from the menu bar
Enter "Explorer.exe" (This start the explorer service)
Repeat the above steps again (This will lauch an instance of Windows Explorer)
The Metro UI should be usable then
Java application as a shell
More to the point, in order to run your java app, change the below entry in the above .bat
from:
c:\Windows\system32\reg.exe ADD "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /t REG_SZ /d "c:\program files\vlc\vlc.exe -f --loop ""d:\media"""
to
c:\Windows\system32\reg.exe ADD "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /t REG_SZ /d "java [any other JVM options you need to give it] -jar "path\jar-file-name.jar""
If your java app does not include a manifest the above will not work!
Try this (I have not tested this):
java -cp jar-file-name.jar full.package.name.ClassName
Revert to the Explorer (default shell) i.e. Undo user-specific shell
To undo the shell changes for the user i.e. revert back to original settings:
!This must be run while logged into the user with the custom shell!
Create a new .bat file:
in Windows Explorer, Select "File" -> "New" -> "Text Document"
Rename "New Text Document.txt" to default_shell.bat
Paste the below into the file
Save the file
Right click the file -> "Run as Administrator"
c:\Windows\system32\reg.exe DELETE "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /f
c:\Windows\system32\reg.exe ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /t REG_SZ /d explorer.exe
c:\Windows\system32\reg.exe DELETE "HKLM\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\system.ini\boot" /v Shell /f
c:\Windows\system32\reg.exe ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\IniFileMapping\system.ini\boot" /v Shell /t REG_SZ /d "SYS:Microsoft\Windows NT\CurrentVersion\Winlogon"
c:\Windows\system32\reg.exe DELETE "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell /f
pause
User Auto Logon:
You can configure Windows to automatically log onto a specific user profile.
Start > Search > netplwiz
OR [Windows Key + R] > netplwiz
A ‘User Account’ window will open. Highlight the account you want to automatically load when Windows starts.
Uncheck the box above it titled “Users must enter a user name and password to use this computer.”
Click OK.
You will be prompted to confirm the operation by entering your password.
Enter your password to complete the change.
The next time you start up Windows, the user account you selected will automatically be loaded
Things to consider
Play around, but use a test environment if possible
If you can launch the shell, so can someone else. Use the keyboard filter to filter out known key combinations, and create one only you, and perhaps the service technicians will know.
Remove admin rights for the user with the custom shell
Use the Unified Write Filter (or Enhanced Wright Filter/File Based Write Filter). Un-protect only when making changes.
A big THANK YOU to Sean Liming and the work that does - he inspired this.
Mark Böhmer
Windows Embedded Specialist
South Africa

Run BAT as admin (w/o shortcut)

So, I am trying to create a .bat to taskkill a specific program in Win7.
I am using the command:
taskkill /f /im "LCore.exe"
The .bat needs to be run as admin in order to work it seems so I have created a shortcut to it to run automatically in admin mode as specified in another thread (How to code a BAT file to always run as admin mode?).
However, when using the Microsoft Mouse and Keyboard Center to map one of my additional keys to run the shortcut it automatically runs the target of the shortcut rather than the shortcut itself which doesn't have the admin privileges needed (when selecting to map the shortcut it automatically changes the path to the target, manually setting the path returns a 'not found' error).
So basically I was wondering if there is another way that doesn't involve creating a shortcut to automatically run a .bat with elevated privileges.
#ECHO OFF
OPENFILES>NUL 2>&1
IF "%ERRORLEVEL%"=="0" GOTO :YouAreAdmin
GOTO :GetAdmin
:GetAdmin
ECHO.Set UAC = CreateObject^("Shell.Application"^) > "StartAsAdmin.vbs"
ECHO.UAC.ShellExecute "%~fs0", "", "", "runas", 1 >> "StartAsAdmin.vbs"
StartAsAdmin.vbs
DEL "StartAsAdmin.vbs"
EXIT /B
:OK
REM Your code starts here!
ECHO.
ECHO. If you see this, you have started as admin this bat file.
PAUSE>NUL
EXIT
Sorry, but I am new! :)
So first, OPENFILES command checks if you have runned program as administrator, like any command that needs administrator privileges. If you type >NUL 2>&1 after a command, CMD will hide any message from that command and will redirect error. So if you have troubles running a simple command action like OPENFILES, the only error that you can have is that batch file doesen't have administrator privileges. And if %ErrorLevel% is 0 (you have no errors running that command), it seems that you started application as administrator, so it steps to :OK label. But if %ErrorLevel% is not 0 (there is a problem), it seems that application doesen't have administrator privileges. So it will start automatly as administrator from a temporary VBScript generated by that batch file, then is started, and then deleted.
In Windows 7 you don't need to write additional scripts, because it has built-in "PowerShell" instrument. Try the following :
powershell.exe start-process taskkill.exe -verb runas -argumentlist '/f /im "LCore.exe"'

Resources