Page 1 of 1

Refresh CMD window to new settings

Posted: 14 Apr 2016 10:04
by Olyrd
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

Re: Refresh CMD window to new settings

Posted: 14 Apr 2016 10:37
by npocmaka_
instead of using registry to edit the settings you can check this thread : viewtopic.php?f=3&t=7047

Re: Refresh CMD window to new settings

Posted: 14 Apr 2016 11:45
by Olyrd
That's awesome but I don't it it fits here. I also want to turn on QuickEdit and enlarge Buffer Size.

Re: Refresh CMD window to new settings

Posted: 14 Apr 2016 13:54
by npocmaka_
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.

Re: Refresh CMD window to new settings

Posted: 14 Apr 2016 14:11
by npocmaka_
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.

Re: Refresh CMD window to new settings

Posted: 15 Apr 2016 05:43
by Olyrd
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?

Re: Refresh CMD window to new settings

Posted: 15 Apr 2016 07:24
by npocmaka_
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.