Want to use some PS command in Cmd script to set a var

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Want to use some PS command in Cmd script to set a var

Post by MigrationUser »

28 Nov 2018 20:08
be

Code: Select all

::  In cmd script, this line results in display of the text
    PowerShell "54321"

::  In PS console, these twp lines
::  result in creation of var and display of its content
    $x = "54321"
    $x

::  In cmd script, these two lines result in nothing.
    PowerShell $x = "54321"
    PowerShell $x
::  The var is not created. No error.
----------------------------

#2 29 Nov 2018 00:38
npocmaka


try like this:

Code: Select all

powershell "$x='123';$x"
or:

Code: Select all

powershell "$x="""123""";$x"
you can even combine powershell and batch code in one file (save this as .bat):

Code: Select all

<# : batch portion
@echo off & setlocal

set "arg=%~1"

powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF

: end batch / begin powershell #>

# display %arg%
$env:arg
$x="123"
$x
----------------------------

#3 30 Nov 2018 20:37
Simon Sheppard


npocmaka has given you some solutions, but to explain - these 2 lines:
be wrote:

PowerShell $x = "54321"
PowerShell $x
Are creating two separate instances of Powershell.

To run the commands in a single process, you need to pass a single script or command to Powershell.
See this page for powershell.exe
https://ss64.com/ps/powershell.html

----------------------------

#4 01 Dec 2018 03:52
be
npocmaka wrote:

powershell "$x='123';$x"
Good.
It demonstrates that the var is created.
Given that the var it is gone after the line, I continued the line with
a long series of commands
in order to have the var for what I wanted to do with it.
npocmaka wrote:

powershell and batch code in one file:

<# : batch portion
@echo off & setlocal
set "arg=%~1"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
# display %arg%
$env:arg
$x="123"
$x
That is very, very clever. And so useful.
I much appreciate that you added it into your reply.

----------------------------

#5 01 Dec 2018 03:55
be
Simon Sheppard wrote:

separate instances of Powershell.
Thanks.

----------------------------

#6 03 Dec 2018 20:22
be


(This occurs also in script.)

DO command in parens, works :
cmd prompt > FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; $x )
5

DO command in parens, fails :
cmd prompt > FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x -gt 1) { $x } )
{ was unexpected at this time.

Same DO command NOT in parens, works :
cmd prompt > FOR /f %a in ('echo 5') DO PowerShell $x = %a ; IF ($x -gt 1) { $x }
5

Explanation ?

Last edited by be (03 Dec 2018 20:23)

----------------------------

#7 03 Dec 2018 21:43
Aacini

Code: Select all

FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x  -gt 1) { $x } )
                      this ^ open paren for CMD is closed by:  ^

You must escape such right paren, so CMD don't "see" it:

FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x  -gt 1^) { $x } )
----------------------------

#8 03 Dec 2018 22:15
be


Thanks, that's it alright. But darn,
Cmd should recognize nested parens pairs.

----------------------------

#9 03 Dec 2018 22:50
Aacini
be wrote:

Cmd should recognize nested parens pairs.
Really? Should it also sintactically parse each command in order to see if there is an unpaired right paren that don't matters? neutral

Code: Select all

for %%a in (A B C) do (
   echo Turn %%a  with right paren ^)
)
Post Reply