Test if batch file is being run under Powershell or Command prompt or Terminal window.
I occasionally make up batch files with a simple menu.
The menus are set to display correctly when the windows "Default profile" is set to Command Prompt, not Powershell and also not run in a "Terminal" window.
Also the Default terminal application" needs to be set to "Windows Console Host".
The simple "Menu" doesn't display correctly if not run under settings as set out above.
So I want to cancel running the batch files if they are running under "Terminal" Window or "Powershell".
Also cancel if the Default terminal application" is not set to "Windows Console Host".
I need to know at the start of batch file if it's set to run as "Terminal" settings below.
Of course I can set out the following instructions in a readme.txt but it's better to be able to test and cancel batch file if it's not running as required below.
Instructions:
#1. Set "Default profile" to Command Prompt. Not Powershell. ... Press Ctrl + Shift + 2 while the Terminal window is in focus.
#2. Set "Default terminal application" to Windows Console Host. Not "Windows Terminal" and not "Let Windows decide".
#3. Select "Save".
The issue is the test batch always returns CMD even if I temporarily use “Terminal” to change to Powershell, it never returns a result of Powershell.
Code piece from internet used to test. A few more in zip plus description.
https://medium.com/@ranemirusG/tell-whether-it-is-cmd-or-powershell-cb157d0d3633
"This little ‘polyglot’ snippet tells us which CLI are we using. It may be useful in situations where we gain access to a Windows system and know nothing about it."
(dir 2>&1 *`|echo CMD Test #1);&<# rem #>echo PowerShell
Not sure where best to post this query, hope here is ok. Thanks.
+ Reply to Thread
Results 1 to 13 of 13
-
Last edited by JN-; 4th May 2026 at 09:54.
-
Using batch scripts, to avoid to all this batch script hacking madness and insanity, what I ended up using, sort of as a "menu", was actually launching notepad files, that could be edited by user (changing options, avisynth lines, etc.) and after saving file and canceling it, batch script would just continue parsing that notepad file and then continuing further in a merry way ....
So been there, done that (doing simple menus), but switched to python and Im just amazed since, it is a ride. You'd get there, and you will enjoy it, guaranteed, in months, doing guis, menus etc., recommend picking up pyqt6 for python (QT 6 version for menu) or tkinter (easier, limitations, but I use it, no problems), -
Thanks _AI_ for your input on Python. I'm unlikely to go down that road. Maybe if I knew of it a few years ago then I may have taken it up.
I do understand that my query is not strictly an Audio/Video one but there are some very knowledgeable people here and so I gave it a try.Last edited by JN-; 14th Jun 2026 at 07:07.
-
I came across another link on stack overflow about this topic. https://stackoverflow.com/questions/7486717/finding-parent-process-id-on-windows/7486816#7486816
The post at the end was the only one that does without WMIC which is depreceated now. Post by Knile.
Get-CimInstance -className win32_process | where-object {$_.ProcessId -eq processId_goes_here } | select ParentProcessId, Name
When I put this in a batch file nothing happens.
Any help from Powershell users is appreciated.
Thanks.
PS
AI came up with this ..
Get-CimInstance Win32_Process -Filter "ProcessId = $PID" | Select-Object -Property ParentProcessId, @{Name="ParentName"; Expression={(Get-Process -Id $_.ParentProcessId).ProcessName}}
When I put this in a batch file nothing happens. If I put Powershell in front of it, the same.Last edited by JN-; 11th Oct 2026 at 20:33.
-
Depending on what version of Windows you are using it may not be able to be done.
MS basically made "Windows Terminal" to be the default, the CMD prompt is nothing more than a "shell environment" (CMD.exe) that emulates the old school "DOS Command Interpreter" in Windows Terminal. That started back around Win7 when the full screen DOS command screen went away.. MS also tightened up security around the CMD prompt by making it run in Windows Terminal so some commands do not have enough "privileges" to execute without elevating privileges.
Might be able to force your script to run CMD outside of Powershell by adding CMD plus needed arguments into your batchfile/script or simply by opening the CMD window and then running your batchfile instead of launching script from Windows GUI. But keep in mind, some things may not function or function the same in the CMD window environment.
I have a DOS program that absolutely requires full screen command and control of the screen, worked fine on XP, unable to get it to work on Win7m tried it on Win10 and still no go even though in Win10 MS conceited and added back the option to full screen the CMD screen..
You can check MS help on CMD https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
If all else fails, perhaps trying a VM with an older OS that worked with your scripts might be in the cards?
Otherwise might need to learn Powershell commands to do what you want.. -
A .bat file always runs under cmd.exe, even when you type its name at a PowerShell prompt (PowerShell just spawns cmd.exe /c yourfile.bat). So the real test is whether the cmd.exe running the batch was launched by PowerShell.Code:
@echo off powershell -NoProfile -Command "$p=(Get-CimInstance Win32_Process -Filter ('ProcessId='+$PID)).ParentProcessId; $g=(Get-CimInstance Win32_Process -Filter ('ProcessId='+$p)).ParentProcessId; $n=(Get-Process -Id $g -ErrorAction SilentlyContinue).ProcessName; if ($n -in 'powershell','pwsh') {exit 1} else {exit 0}" if errorlevel 1 ( echo Launched from PowerShell ) else ( echo Launched from Command Prompt ) -
Thanks GAHere and Columbo for your responses..
I had given up on this, the query is up a long time.
A few days ago I tried ChatGPT.
My main reason for all of this is to make sure that my full screen batch files simple key only on screen menu appears as I laid it out when I set it up on a FHD screen, outside of any Terminal or PS window. PS window uses different font size to Terminal. Best to not use either window.
A powershell or Terminal window might have to be resized to achieve that, so best to avoid.
ChatGPT came up with a solution for testing if Terminal window session is running. Although system wide, this resolves the Terminal window issue.
The Powershell issue, it turns out it probably wasn't necessary to chase up at all! As per ChatGPT ...
Test Expected result
-----------------------------------------------------------------------------------------
Double-click the batch file in File Explorer ..... Runs normally
Launch from a traditional CMD window ..... Runs normally
Launch from PowerShell outside Windows Terminal ..... Runs normally
Launch from Windows Terminal with a CMD profile ..... Refuses when WT_SESSION is present
Launch from Windows Terminal with a PowerShell profile ..... Refuses when WT_SESSION is present
-----------------------------------------------------------------------------------------
See Columbo post page #7. that confirms it.
"A .bat file always runs under cmd.exe, even when you type its name at a PowerShell prompt (PowerShell just spawns cmd.exe /c yourfile.bat). So the real test is whether the cmd.exe running the batch was launched by PowerShell."Last edited by JN-; 17th Oct 2026 at 14:52.
-
This is wwhat I now use at start of my batch files ...
REM ************************************************** ************************************************** ************************************************** ****** Terminal test Start
powershell.exe -NoProfile -Command "if (Get-Process -Name WindowsTerminal -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
if %ERRORLEVEL% EQU 0 (
echo
echo.
echo ================================================== ================================================== =====================================
REM An errorlevel of 0 means Terminal is in use.
echo.
echo Windows Terminal detected. Cancelling. This batch util will best display when not run under a Terminal window.
echo Note that it can be anywhere on the system. Close it or do the following.
echo See also the text file "# Readme 1st, using Win 11, Powershell etc.txt".
echo.
echo To change the "Default terminal application" to Windows Console Host and not "Windows Terminal" and also not "Let Windows decide".
echo.
echo Run Terminal. Click the small "v" item at the top. Select "Settings".
echo.
echo Set "Default terminal application" to Windows Console Host. Not "Windows Terminal" and NOT "Let Windows decide".
echo.
echo Select "Save" at bottom of window.
echo.
echo It's not essential but preferable to also set the "Default profile" to Command Prompt, Ctrl+Shift+2. Not Powershell, Ctrl+Shift+1.
echo That's if it's not already set to Command Prompt. Then select "Save".
echo.
echo ================================================== ================================================== =======================================
echo.
echo Press any key to exit ...
pause>nul
color
exit
) else (
REM An errorlevel of 1 means Terminal is NOT in use.
)
REM ************************************************** ************************************************** ************************************************** ****** Terminal test End
I'll check out Columbo's script later on, thanks. -
I get this error when I run t.bat in PS window. Have to enter as ".\t.bat"\.
T.bat is ...
powershell -NoProfile -Command "$p=(Get-CimInstance Win32_Process -Filter ('ProcessId='+$PID)).ParentProcessId; $g=(Get-CimInstance Win32_Process -Filter ('ProcessId='+$p)).ParentProcessId; $n=(Get-Process -Id $g -ErrorAction SilentlyContinue).ProcessName; if ($n -in 'powershell','pwsh') {exit 1} else {exit 0}"
if errorlevel 1 (
echo Launched from PowerShell
) else (
echo Launched from Command Prompt
)
echo.
pause
exit
At line:1 char:11
+ ".\t.bat".
+ ~
Missing property name after reference operator.
+ CategoryInfo : ParserError: (
[], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingPropertyName -
That error comes from how the command was typed at the PowerShell prompt, not from the batch file. The parser shows the input as:
".\t.bat".
There are two problems:
Trailing period. In PowerShell a . after a value is the member access operator, so it expects a property name after it. That is exactly what “Missing property name after reference operator” means.
Quotes. A quoted path on its own is just a string. PowerShell echoes it instead of running it.
Type it as:
.\t.bat
If the path ever needs quotes (spaces in it), use the call operator:
& ".\t.bat"
Requiring .\ is normal. PowerShell does not run scripts from the current directory without an explicit path. -
Absolutely Brilliant.
That's a solution plus the CHATGPT Terminal test.
If there are spaces in the filename of say a test file named A test.bat type .\"A test".bat and press Enter.Last edited by JN-; 17th Oct 2026 at 16:48.
Similar Threads
-
Using Powershell how to test if batch file var is greater than a set value
By JN- in forum Newbie / General discussionsReplies: 10Last Post: 16th Mar 2026, 15:59 -
Get all available graphic cards & put into batch variable using Powershell
By JN- in forum Newbie / General discussionsReplies: 11Last Post: 6th Oct 2025, 15:26 -
Unable to Load DLL File into Command Prompt
By jealousy91 in forum Software PlayingReplies: 1Last Post: 3rd Nov 2021, 14:14 -
Avfs.dll Not Installing When Running on Command Prompt
By jealousy91 in forum Video ConversionReplies: 3Last Post: 26th Oct 2021, 06:58 -
How to get started with youtube-dl from the command prompt
By Baldrick in forum User guidesReplies: 25Last Post: 3rd Jun 2021, 22:13



Quote