-1

I want to run .bat-script which calls some powershell function inside it. Function is not so small, so I want to split it. But I cannot do it, escape symbols doesn`t help ( ` ,^). Script example:

set file=%1

set function="$file=$Env:file; ^
              $hash = CertUtil -hashfile $file SHA256 | Select -Index 1"

powershell -command %function%
5
  • 4
    you can't use Powershell syntax in batchfiles. $function=... doesn't work in batch. Commented Aug 9, 2018 at 13:01
  • @Stephan of cource, my mistake, but problem was not connected with it Commented Aug 9, 2018 at 13:31
  • no, it's connected with the quotes. Without quotes (like in @Rodby's answer), it works. Commented Aug 9, 2018 at 13:33
  • Yes, with quotes but not with $function= construction :) Commented Aug 9, 2018 at 13:35
  • 2
    See this answer Commented Aug 9, 2018 at 13:37

2 Answers 2

4

You can leave the quote at the end of each line like so:

set file=%1

set function="$file=$Env:file; "^
           "$hash = CertUtil -hashfile $file SHA256 | Select -Index 1; "^
           "example break line further...."

powershell -command %function%

The ^ works as multiline character but it also escapes the first character, so also a quote would be escaped.

Sign up to request clarification or add additional context in comments.

3 Comments

Perhaps it'd be nice to update the answer as per edits to the question (set function and %function%) so that it actually works :)
I have updated the answer didn't relize the powershell syntax to be honest
Awesome @Wojtek-t! Just a heads up, it's usually worth to test (if possible) before posting :)
1

Do not mix batchfile syntax with PowerShell. As @Stephan mentioned $function= won't work in batch file. You need to use set function= instead. Let's say I want to execute the following:

Get-Process
Get-ChildItem

Then the code should look like this:

set function=Get-Process; ^
Get-ChildItem;

And you start PowerShell with:

powershell -noexit -command %function%

-noexit added so that you can verify that the code was successfully executed.

Also keep in mind that what you pass to PowerShell is batch multiline and in PowerShell it's visible as one line so you have to remember about semicolon (which you actually do but I'm leaving this comment here for future readers).


There's also another option how to pass variable from batch script to PowerShell. You can do it like this:

set name=explorer

set function=get-process $args[0]; ^
get-childitem

powershell -noexit  -command "& {%function% }" %name%

Explanation:

$args[0] represents first argument passed to the scriptblock. To pass that argument, add %name% after the scriptblock while starting powershell. Also, as pointed out in this answer (credits to @Aacini for pointing this out in comments), you have to add & operator and keep your scriptblock inside curly brackets { }.


Sidenote: to be honest, I'd avoid running scripts like this. Much simpler way would be to just save the file as .ps1 and run this in your batch file:

powershell -noexit -file .\script.ps1

1 Comment

I agree, that`s bad practice. But I do need to put powershell code inside batch-file, that`s my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.