How to reset a batch variable in Powershell?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 525
Joined: 07 Jan 2016 07:47

How to reset a batch variable in Powershell?

#1 Post by SIMMS7400 » 31 Aug 2021 07:34

Hi Team -

I have a process where I set a variable in batch but then want to reset back to another value from a PowerShell process that I'm executing but can't seem to get it to work.

Here is my logic:

Code: Select all

SET "PRE_NOTIFY=T" & CALL :EMAIL
Which calls this:

Code: Select all

:EMAIL

::-- Set Distribution List --::
IF NOT DEFINED ERR SET "EMAIL_TO=%EMAIL_DL_ADMIN%"
IF DEFINED ERR SET "EMAIL_TO=%EMAIL_DL_ADMIN%"

POWERSHELL -ExecutionPolicy ByPass -file "%EMAIL_BIN%Email_Utility.ps1" ^
	"%EMAIL_SERVER%" ^
	"%EMAIL_FROM%" ^
	"%EMAIL_TO%" ^
	"%EMAIL_PSSWD%" ^
	"%EMAIL_PORT%"

CLS
GOTO :EOF
And then in PowerShell, I try to do :

Code: Select all

$Env:PRE_NOTIFY "updated"
among others things but no luck when I try to echo the variable back in my batch file. Any ideas?

aGerman
Expert
Posts: 4289
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to reset a batch variable in Powershell?

#2 Post by aGerman » 31 Aug 2021 11:36

A process inherits a snapshot of the environment from its parent process at the time it is spawned. That's a one way. Updates of the environment in the child process will never affect the parent process. (And later updates of the parent environment will not reach out to the child anymore.) So, no chance for your approach to succeed. Use a temporary file, or maybe the registry.

Steffen

siberia-man
Posts: 187
Joined: 26 Dec 2013 09:28
Contact:

Re: How to reset a batch variable in Powershell?

#3 Post by siberia-man » 03 Sep 2021 10:04

I guess the following experiment covers your requirements. But pay your attention that deleting environment variable works only within PS. Deletion doesn't propagate to upper process (CMD in your case). So when you return to CMD, you'll see the original value:

Set the variable in CMD and call PS

Code: Select all

C:\>set XXX=qwe

C:\>powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Check the variable and change it in PS

Code: Select all

PS C:\> $Env:XXX
qwe
PS C:\> $Env:XXX = "asd"
PS C:\> $Env:XXX
asd
PS C:\> $Env:XXX.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
Still under PS delete it now

Code: Select all

PS C:\> remove-item Env:XXX
PS C:\> $Env:XXX.gettype()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $Env:XXX.gettype()
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PS C:\>
Now return to CMD and check the original value

Code: Select all

PS C:\> exit

C:\>set XXX
XXX=qwe

T3RRY
Posts: 181
Joined: 06 May 2020 10:14

Re: How to reset a batch variable in Powershell?

#4 Post by T3RRY » 03 Sep 2021 11:31

Just an idea -
Consider modifying the ps1 file to write the updated variable value to the host and use a for /f loop to capture that value for assignment to a variable in the parent batch environment.
If your script outputs other data to the host, prepend output of the update value with a string and delimiter to identify and isolate the desired output with the aid of findstr and the delims option of the for /f loop.

Post Reply