Refresh CMD window to new settings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Refresh CMD window to new settings

#1 Post by Olyrd » 14 Apr 2016 10:04

I'm using the following code to tweak CMD settings and to automatically run it as administrator:

Code: Select all

CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B

:gotPrivileges
setlocal & pushd %~dp0




REM Options Tab
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v QuickEdit /t REG_DWORD /d 0x00000001 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v InsertMode /t REG_DWORD /d 0x00000001 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v HistoryBufferSize /t REG_DWORD /d 0x000003e7 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v NumberOfHistoryBuffers /t REG_DWORD /d 0x00000005 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v HistoryNoDup /t REG_DWORD /d 0x00000000 /f

REM Font Tab
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v FaceName /t REG_SZ /d Consolas /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v FontFamily /t REG_DWORD /d 0x00000036 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v FontSize /t REG_DWORD /d 0x000e0000 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v FontWeight /t REG_DWORD /d 0x00000190 /f

REM Layout Tab
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v ScreenBufferSize /t REG_DWORD /d 0x270f00b4 /f
REG ADD "HKCU\Console\%%SystemRoot%%_system32_cmd.exe" /v WindowSize /t REG_DWORD /d 0x001e00b4 /f




FOR /F "skip=1 tokens=1,2,* delims= " %%I IN ('qwinsta console') DO SET currentuser=%%J
set "home=C:\Users\%currentuser%"


doskey /exename=cmd.exe cd=cd /d $*
doskey /exename=cmd.exe ls=dir/b
doskey /exename=cmd.exe tc=ping 8.8.8.8 -t
doskey /exename=cmd.exe rdp=mstsc.exe /v:$1 /f
doskey /exename=cmd.exe note=notepad.exe
doskey /exename=cmd.exe print=notepad.exe C:\Windows\System32\drivers\etc\hosts
doskey /exename=cmd.exe reboot=shutdown -r -t 0 -f
doskey /exename=cmd.exe shutdown=shutdown -s -t 0 -f

doskey /exename=cmd.exe ~=cd /d "%home%\Desktop"

cmd /k cls



The expected resut is to be able to set the new settings, run as administrator, and refresh the default CMD window to the new width, height, Quick Edit mode set in the script.

Any insight?
Thank you

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Refresh CMD window to new settings

#2 Post by npocmaka_ » 14 Apr 2016 10:37

instead of using registry to edit the settings you can check this thread : viewtopic.php?f=3&t=7047

Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Re: Refresh CMD window to new settings

#3 Post by Olyrd » 14 Apr 2016 11:45

That's awesome but I don't it it fits here. I also want to turn on QuickEdit and enlarge Buffer Size.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Refresh CMD window to new settings

#4 Post by npocmaka_ » 14 Apr 2016 13:54

To enable/disable quick edit mode you can chech this:

Code: Select all

Enable:
 quickEdit  1
Disable:
 quickEdit  2
Get State:
 quickEdit  3


buffer size can be changed with the dbenham's script.

Changing the font is possible with c# but I have no such script (yet). If you google a little bit you'll find some solutions and can create similar to quickEdit.bat script.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Refresh CMD window to new settings

#5 Post by npocmaka_ » 14 Apr 2016 14:11

To refresh the settings set through the registry you'll need to start completely different console - but not a sub-process of already started script (otherwise the envintonment will be inherited)
You can do this with SCHTASKS (the only option that comes to me at the moment).
Or to use console classwith powershell,jscript or c#.
But not everything that can be done with the console is covered with the .net class and for things like quick edit mode and fonts you'll have to use platform invoke (mainly with c#).
The good thing with the platform invoke is that the changes are immideate.

Olyrd
Posts: 25
Joined: 05 Feb 2016 07:36

Re: Refresh CMD window to new settings

#6 Post by Olyrd » 15 Apr 2016 05:43

SCHTASKS will do the trick but how to make it appear the CMD window? I mean any batch script eneterd in a task will be run hidden. Any way to make it visible?

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Refresh CMD window to new settings

#7 Post by npocmaka_ » 15 Apr 2016 07:24

I don't think so.
Here's an example. This batch will ran itself through SCHTASKS with additional argument to check if it is started through schtasks:


Code: Select all

@echo off

if "%~1" neq "schtask" (
    SCHTASKS /create /tn "%~nx0" /tr "%~f0 schtask %*" /sc ONCE /sd 01/01/1910 /st 00:00 /RU "%username%" /f
    SCHTASKS /Run /TN "%~nx0"
    exit /b %errorlevel%
)
shift
echo ----
pause


But still I would prefer other .NET solutions as with this you'll have a lot of limitations and changes affects only the current cmd session.

Post Reply