TCC and PowerShell shared environment

I discovered something by accident today.

I am using;
Code:
c:\users\jlc\utils>ver

TCC  24.02.46 x64   Windows 7 [Version 6.1.7601]
In my Powershell profile, C:\Users\jlc\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1, I have, for example, the following function;
Code:
function CDate
{
  param ([string]$theDate)
  $bdate = Get-Date -format D $theDate
  $env:cdate=$bdate
}
If I do the following from TCC;
Code:
c:\users\jlc\utils>pshell /c

c:\users\jlc\utils>pshell /s "cdate %_isodate"

c:\users\jlc\utils>echo %cdate
Saturday, March 30, 2019
...the environment variable that I just set in PowerShell via my CDate function is also set in TCC.

Now, if I do;
Code:
c:\users\jlc\utils>pshell /c
followed by;
Code:
c:\users\jlc\utils>pshell /s "$env:cdate"
Saturday, March 30, 2019
...the environment variable is still set in Powershell, even though I have closed the Powershell interpreter.

From TCC, if I do;
Code:
c:\users\jlc\utils>set cdate=jajb

c:\users\jlc\utils>echo %cdate
jajb
...then from TCC;
Code:
c:\users\jlc\utils>pshell /s "$env:cdate"
jajb
...the environment variable that I just set in TCC has also been set in PowerShell.

I have no problem with this, as it eliminates the use of @execstr for getting the output of a PowerShell function, but is this WAD?

Joe
 
May 20, 2008
9,274
62
Syracuse, NY, USA
It doesn't surprise me. An environment belongs to a process. In all the above, the only process is TCC. If you were invoking POWERSHELL.EXE it would be a different story.
 
Aug 23, 2010
354
5
That's not necessarily true. Usually, Windows programs do not modify their own environment block.
Preparing new environment block for spawned processes is more common.