views:

223

answers:

3

Is there any way a batch script can know if it's called from PowerShell (without extra parameters feeded)?

Need something like..

if (%THIS_BATCH_CALLED_FROM_POWERSHELL%)
    ... warn the user or drop back to powershell to execute the proper instructions...

Question related with this question - virtualenv-in-powershell.

A: 

You could add a default warning in the script and pass it a flag that tells it not to show the warning. When you call it from power shell pass it that flag.

JustSmith
The intention is to do this without a flag. Thanks.
utku_karatas
A: 

In my Powershell environment (a PS 2.0 CTP), I seem to have an environment variable PSMODULEPATH which is not set by the normal command line environment, but still exists when Powershell has a child CMD.exe shell.

I think you might be able to "reliably enough" check for the existence of PSMODULEPATH in your batch script.

JasonTrue
Are you sure? %PSMODULEPATH% seems to be a global env. setting and thus exists on cmd.exe shell too on my machine.
utku_karatas
Not sure, as I only have my machine to check against, but when I run "CMD.exe" from Explorer, it doesn't exist. When I run Powershell, it does exist. When I run cmd.exe from Powershell, the environment is copied into the new cmd.exe and it exists there.
JasonTrue
%PSMODULEPATH% is a global env variable for me on Win7.
Richard Berg
Yes, unfortunately for me too, now that I look at my Win7 machine...
JasonTrue
+3  A: 

You could use a tool like "tlist.exe /t" or this one to display the PIDs of the current process and all parent processes. You could check each one of those PIDs to see if any correspond to PowerShell.exe.

Keith Hill
That's an ugly way but at least does the job. Thank you Keith.
utku_karatas