Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling PowerShell from batch, and retrieving the new value of a temporary environment variable set in the script?

I hope the title is concise, but just in case:

I am calling a PowerShell script from a batch file. I want the PowerShell script to set the value of an environment variable, and for that new value to be available in the batch file when the PowerShell script finishes.

I know that it is possible to set an environment variable using $env in PowerShell, but the value does not persist when the PowerShell script terminates. I imagine this is probably because PowerShell gets executed in a separate process.

I am aware that I can return an exit code and use %ErrorLevel%, but that will only give me numbers, and there will be a conflict, since 1 indicates a PowerShell exception rather than a useful number.

Now, here's the caveat: I don't want the environment variable to persist. That is, I don't want it to be defined for the user or system, and therefore I want it to be unavailable as soon as the batch file exits. Ultimately, I simply want to communicate results back from a PowerShell script to the calling batch file.

Is this possible?

Thanks in advance :)

Nick

like image 613
Nicholas Hill Avatar asked Aug 05 '10 11:08

Nicholas Hill


People also ask

How do you call an environment variable in PowerShell?

We can also access environment variables using the built-in variable called $env followed by a colon and the name of the environment variable. For example, instead of using Get-Item or Get-ChildItem and using the Env drive, I can save some keystrokes and instead simply specify $env:COMPUTERNAME.

How do I set an environment variable in a batch file?

When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set. If you call a variable value from a batch file, enclose the value with percent signs (%).

What is $_ in PowerShell script?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

Why we use $_ in PowerShell?

$_ is a variable created by the system usually inside block expressions that are referenced by cmdlets that are used with pipe such as Where-Object and ForEach-Object . But it can be used also in other types of expressions, for example with Select-Object combined with expression properties.


1 Answers

To get Keith's idea of using stdout to work, you can invoke powershell from your batch script like this:

FOR /F "usebackq delims=" %v IN (`powershell -noprofile "& { get-date }"`) DO set "d=%v"

A little awkward, but it works:

C:\>FOR /F "usebackq delims=" %v IN (`powershell -noprofile "& { get-date }"`) DO set "d=%v"
C:\>set d
d=August 5, 2010 11:04:36 AM
like image 68
zdan Avatar answered Oct 14 '22 23:10

zdan