Has anyone figured out how to write to and read Windows 11 Terminal?

Just starting out? Need help? Post your questions and find answers here.
MinerDac
User
User
Posts: 11
Joined: Sat Jul 13, 2024 12:02 pm

Has anyone figured out how to write to and read Windows 11 Terminal?

Post by MinerDac »

In Windows 10 I could do this for PowerShell:

Code: Select all

; example version
; psrun$ = "pwsh.exe" ; for powershell 7
psrun$ = "powershell.exe" ; for native PS 5

a$ = " <some ps command code here>;"
b$ = " <some ps command code here>;"
c$ = " <some ps command code here - and what ever the return is>;"

cmdx$ = "$ErrorActionPreference = 'SilentlyContinue';" + a$ + b$ + c$ ; etc...
  OutProg$ = ""
  KeepOutProg$ = ""
  ProgRun = RunProgram(psrun$, cmdx$,"", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
  If ProgRun
    While ProgramRunning(ProgRun)
      OutProg$=ReadProgramString(ProgRun)
      KeepOutProg$ = KeepOutProg$ + OutProg$
    Wend
    CloseProgram(ProgRun)
  EndIf
  
  ProcedureReturn = Trim(KeepOutProg$)
Now, that being said ... Windows 11 does have powershell installed and I have powershell 7 installed and the above works fine as it is. BUT! Since MS is moving towards a complete elimination of a separate powershell and command prompt and the functionality of these would be rolled into Windows Terminal in the future I'm kinda wondering how to start using Windows Terminal for the same functionality I get now from using the separate powershells.

Yes, you can run powershell code in Windows Terminal in Windows 11, the executable is called wt.exe. But whats its doing for right now is actually using powershell in the background. So I wanted to try using Windows Terminal to see if I can start modifying my code for my powershell stuff to use wt.exe and not powershell 5 or 7. So far I have not been successful - It runs wt.exe fine but there is never any return from Terminal and I'm not sure its even sending the code to terminal even with the #PB_Program_Write flag used (yes I tried various incarnations, and also using WriteProgramData,
WriteProgramString, WriteProgramStringN.

So what am I doing wrong - how is it done?
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Has anyone figured out how to write to and read Windows 11 Terminal?

Post by Randy Walker »

Maybe too nitty gritty? I skipped past Powershell and just treat the command as an actual program. I wanted to get CPU speed and web search gave me this neat little command line:

Code: Select all

wmic cpu get name,currentclockspeed
I just plugged that into the Runprogam() command and worked great. The ReadProgramString() is used to collect output from the "wmic" command. Run the following code with debugger enabled:

Code: Select all

  Exe=RunProgram("wmic","cpu get name,currentclockspeed","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error)
  If Exe
    While ProgramRunning(Exe)
      If AvailableProgramOutput(Exe)
        Output$=ReadProgramString(Exe) ;trash first (header) line
        Output$=Left(ReadProgramString(Exe),5) ; Capture 2nd line
        CPU = Val(LTrim(Output$))              ; retain first word only
        If CPU
          Debug CPU
        EndIf
      EndIf
    Wend
  EndIf
Interestingly, I did a little substitution to get a "Dir" listing and came up dry, and don't know why, unless maybe the "Dir" command runs too fast for PB to keep up with it. So it seems because the "Exe" variable is Null straight out of the runprogram command:

Code: Select all

Exe = RunProgram("Dir","C:\Windows\","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error)
Debug Exe
If Exe
    While ProgramRunning(Exe)
      If AvailableProgramOutput(Exe)
        Output$=ReadProgramString(Exe)
        Debug Output$
      EndIf
    Wend
EndIf
The help file says:
RunProgram "returns Nonzero if the program has been successfully launched, zero otherwise."
So I guess really RunProgram is unable to do some line commands. I don't get it. Then I tried using the "Cmd" command in front of "Dir" and it worked:

Code: Select all

Exe = RunProgram("Cmd"," /C Dir C:\Windows\","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Ascii)
If Exe
    While ProgramRunning(Exe)
      If AvailableProgramOutput(Exe)
        Output$=ReadProgramString(Exe)
        Debug Output$
      EndIf
    Wend
EndIf
NOTE: the Cmd command requires /C to run another command so at the command prompt you would actually type:

Code: Select all

cmd /C Dir C:\Windows\
Not a programmer but sometimes I manage to hack out a solution. Cmd /? gave me the clue I needed :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Has anyone figured out how to write to and read Windows 11 Terminal?

Post by Randy Walker »

Then again, after further research, it seems you may only need to preface your parameters with the -Command switch. Go to your powershell prompt and type:
powershell.exe /?

So you do something like this:

Code: Select all

Exe=RunProgram("powershell.exe"," -Command Dir C:\Windows\ ","") ;,#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error)
Notice how I preface the parameters with the ^^^^^ -Command switch.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply