Release/Reset PowerShell Buffer Size back to CMD Prompt


  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

    Release/Reset PowerShell Buffer Size back to CMD Prompt


    Good evening.

    I have a Batch Script where I set the CMD Prompt Window to open FULL size using this . . .

    Code:
    
    if not "%1"=="max" start /max cmd /c %0 max & exit /b

    Some of the Reports/Logs that I run uses PowerShell Commands. I set the Buffer Size to the following so that the FULL OUTPUT gets written to the Report/Log. The thing is, when the Commands exits, it mucks up the CMD Prompt Window. It does NOT happen when I use PowerShell Commands on their OWN, it ONLY happens when I use PowerShell Commands with the following line . . .

    Code:
    
    PowerShell $Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(250,1000)

    How do I Release/Reset the PowerShell Buffer Size so it returns to what it was previously in the CMD Prompt Window.

    I have obviously tried mode: etc.

    I use the PowerShell Commands [ although I am NOT up with PowerShell, hence the request for help ] because it is more flexible and gives better Results and Output. I do NOT want to find a workaround using the Command Line because I have already been through that process, hence why I am using PowerShell.

    Thanks.
    Last edited by Paul Black; 13 Oct 2021 at 01:35.
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #2

    OK, this problem might NOT be directly connected to PowerShell, although an answer to my above question would be nice.

    After a LOT of time troubleshooting, I have narrowed the weird behaviour down to TWO echo. in the code.

    Here is the test file. If you run the code you will see what I mean. I have quoted the TWO lines and the operation needed to make it work . . .

    Code:
    
    @echo off
    if not "%1"=="max" start /max cmd /c %0 max & exit /b
    
    setlocal EnableDelayedExpansion
    set "Params=%*"
    cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %SystemDrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %Params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)
    %windir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )
    
    set "DT=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
    for /f "usebackq tokens=3" %%X in (`Reg Query "%DT%" /V Desktop`) do (set Desktop=%%X)
    for /f "usebackq delims=" %%X in (`echo "%Desktop%"`) do (set "Desktop=%%X")
    set "Desktop=%Desktop:~1,-1%"
    
    Set "Folder_Name=Sys_Reports"
    set "DT_Folder=%Desktop%\%Folder_Name%"
    
    set "LOG_01=Appx_Packages_Current_User"
    
    :Options
    
    set "Input= "
    echo. & echo  ======================================================================================================================================================================
            echo  some info here
            echo  ======================================================================================================================================================================
    echo. & echo  IF YOU TAKE OUT THE echo. AND LEAVE JUST THE echo ON THIS LINE AND THE NEXT IT WORKS
    echo. & echo  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
            echo  some info here
            echo  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
            echo   0. EXIT this Program
            echo   1. 
            echo   2. 
            echo   3. 
            echo   4. 
            echo   5.
            echo   6.
            echo   7.
            echo   8
            echo   9
            echo   10.
            echo   11.
            echo   12.
            echo   13.
            echo   14.
            echo   15.
            echo   16.
            echo   17.
            echo   18.
            echo   19.
            echo   20.
            echo   21.
            echo   23.
            echo   24.
            echo   25.
            echo   26.
            echo   27.
            echo   28.
            echo   29.
            echo   30.
            echo   31.
            echo   32.
            echo   33.
            echo   34.
            echo   35.
            echo   36.
            echo   37.
            echo   38.
            echo   39.
            echo   40.
            echo  ======================================================================================================================================================================&echo.
    
    set "Input="
    set /P "Input=>Enter an Option and press <Enter>: "
    
    if "%Input%"==""  cls & goto :Options
    if /i %Input%==0  Exit
    if /i %Input%==1  goto :AppxPackages_Current
    goto :Options
    
    :AppxPackages_Current
    
    set "Title=Appx Packages - Current User"
    set "LOG=Appx_Packages_Current_User.log"
    set "OFN=%DT_Folder%\%LOG%"
    if not exist %DT_Folder% (mkdir %DT_Folder% >nul 2>&1)
    if exist %OFN% (del /f /q %OFN% >nul 2>&1)
     echo. & echo  Processing . . .
    >> %OFN% echo ============================================================================= START ==================================================================================
                  PowerShell $Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(250,250)
    >> %OFN%      PowerShell Get-AppxPackage -User %UserName% ^| Sort-Object -Property Name ^| Select Name, PackageFullName, Publisher, PublisherId, ResourceID ^| Format-Table
    >> %OFN% echo ============================================================================== EOF ===================================================================================
             echo  Processing Complete.
    echo. & echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options

    AFTER you have run Option 1, if you scroll down, you will see an EXTRA
    >Enter an Option and press <Enter>:. As you scroll down further, it remains the last line on the screen.

    I wonder if it is ME, or if there really is a problem!

    I am NOT interested in adapting or modifying the code, just WHY this is an issue please.

    Thanks.

    EDIT:

    I have just run this on another computer and it is fine. Maybe it is a problem with my Desktop, which is LONG OVERDUE for a Reinstall. Out of curiosity, I would be interested if the issue happens to ANYONE else though. I have NEVER had this issue before.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 03:21.
Find Us




Windows 10 Forums