1

I want to run a few PowerShell commands through a batch file. Very similar question has been asked but I dont want to run a seperate shell file from a batch. Instead I want to embed PowerShell commands to a batch file.

When I try to run

powershell -command "&{$var = "something"}"

I get the following error:

something : The term 'something' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:1 char:10

&{$var = something}

CategoryInfo : ObjectNotFound: (something:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException

But if I run something like

powershell -command "&{echo "something"}" 

Then everything is fine :/

Am I doing a syntax error or something? And please don't give answers like "Instead of using PowerShell commands use batch commands etc..."

Thanks in advance!

Community
  • 1
  • 1
ozcanovunc
  • 703
  • 1
  • 8
  • 29
  • 1
    you could either use a single quote on 'something' or powershell -command '...' – Martin Brandl Jul 08 '15 at 14:33
  • In your example you put quotes around "something" the error indicates that you don't in your actual code. – EBGreen Jul 08 '15 at 14:36
  • possible duplicate of [How to run a PowerShell script within a DOS batch file](http://stackoverflow.com/questions/2609985/how-to-run-a-powershell-script-within-a-dos-batch-file) – SomethingDark Jul 08 '15 at 20:45
  • Also, check out [this DosTips post](http://www.dostips.com/forum/viewtopic.php?p=37780#p37780) for a converter script. – SomethingDark Jul 08 '15 at 20:45

2 Answers2

0

You can not use nested quotes when defining the value of a variable as a string. You may either use apostrophes instead of the nested quotes:

powershell -command "&{$var = 'something'; echo $var}"

... or escape the nested quotes:

powershell -command "&{$var = \"something\"; echo $var}"

Another example:

powershell -command "&{$var = 'one'+\" two\"; echo $var}"
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Maybe a bit late but I program pure powershell scripts and embed the complete script in a .cmd file to bypass the execution policy setting. I have 2 variants pick the one you like. Both are one-liners that you put on the first line of the .cmd file. Starting from the second line you just program in pure powershell. No need for modifying anything in your powershell script.

Variant1:

@type "%0" | findstr /v "^@type \"%0\" | findstr /v " | PowerShell.exe -noprofile - & goto :eof

Varant2:

@powershell -command "(Get-Content '%0') | select -skip 1 " | powershell -noprofile - & goto :eof
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Variant2 does not seem to work with curly braces in the powershell part of the script – Ryan Williams Mar 29 '21 at 03:08
  • Using a tmp file instead of STDOUT worked for me @powershell -command "$tmpDir = [System.IO.Path]::GetTempPath(); Get-Content '%0' | Select -Skip 1 | Out-File $tmpDir\script.ps1; powershell -noprofile -File $tmpDir\script.ps1; Remove-Item $tmpDir -Recurse" & goto :eof – Ryan Williams Mar 29 '21 at 03:27