0

i have the following script:

@echo off

title cpm
color e

powershell -command "& cls; `
Write-Host write anything:; `
echo "hello""

It won't let me execute it that way, how do I escape the blank line so it can be executed? I have tried with \ and with ^^ and neither, if I put it in a single line it runs perfectly

3
  • In Powershell, the escape character is ` Jul 27, 2022 at 12:33
  • This syntax seems to work fine as long as you run it from a Powershell.exe terminal rather than from cmd.exe or its terminal/shell. i.imgur.com/G4f5lyO.png Something to play with maybe if nothing more. I wasn't able to figure out the cmd.exe synatex to make it work and don't have time to dig more than that at the moment. But with PowerShell, you don't need to be concerned with line breaks or carriage returns for commands on separate lines. Just be sure to end the command line with a semicolon and all should be fine there regarding commands that execute one after another separated lines Jul 27, 2022 at 17:07
  • it doesn't work for me, i have to run it directly from cmd
    – zeros
    Jul 28, 2022 at 6:23

3 Answers 3

1

As per my comment, from your other post, where I provide feedback. This is what I mean.

The below will work as shown...

@echo off
cls
::
title cpm
color e
::
::
cls
PowerShell -Command 'Hello from PowerShell^';^
Get-Date^;^
(Get-Process)[0]
::
@echo off
echo "hello from cmd"

:: Begin Results =========================================
:: D:\Scripts>PowerShell -Command 'Hello from PowerShell';Get-Date;(Get-Process)[0]
:: Hello from PowerShell
:: 
:: Saturday, August 6, 2022 00:30:14
:: 
:: Id      : 10508
:: Handles : 224
:: CPU     :
:: SI      : 0
:: Name    : aesm_service
:: 
:: 
:: 
:: "hello from cmd"
:: End Results =========================================

...but, if you'd say try and us a Select-Object command on that 'Get-Process', it will fail on that. Again, the more complicated the PS command you try and fire off, the more issues (formatting and execution ), you are going to encounter.

So, I cannot fathom why the bat/cmd, with embedded PS code, but, again, just write a .ps1 and call it from your bat/cmd file via PS. You'll have far more flexibility and readability options and far fewer headaches and unreasonable code statements.

0

I am not sure of your intent but the command line Powershell commands are a little off. I believe you want something similar to what I have below, but you may want to move the clear screen call "& cls" below the echo "hello". Also, clearing the screen is also native to the command line, just use cls (you don't need to use Powershell):

@echo off

title ControlMantis
color e

set RUTATEMP="%temp%\pass.pass"

powershell -command "& cls";
powershell -command Write-Host "write anything:";
echo "hello"

You can also chain these PS commands together:

powershell -command "& cls"; Write-Host "write anything 1:"; Write-Host "write anything 2:"; Write-Host "write anything 3:";
5
  • I need to write a powershell code statement not to call powershell 50 times
    – zeros
    Jul 28, 2022 at 6:24
  • I added info on how to chain commands together but that is really a lmgtfy Run multiple commands in one line in Powershell and Cmd
    – DBADon
    Jul 28, 2022 at 14:34
  • read the question please, I need to execute a block of powershell code and to make it more readable I need to do it with line breaks, in a command without line breaks it works perfectly
    – zeros
    Jul 28, 2022 at 14:55
  • 1
    But your issue is that you are using a bat/cmd command to call PS. Your issue is not PS, it's your bat/cmd. Your script as written is controlled by cmd, not PS. You have to be in PS to run PS code. Your bat/cmd code will run in the cmd terminal, and when you call PS, it will start the PS terminal, run the PS code in that, and return control back to the bat/cmd terminal. Unless you pipe the PS stdout back to the cmd terminal, you'll never see it. Formatting for readability in bat/cmd with PS is a bat/cmd issue. Why not just call a .ps1 from bat vs trying to embed PS formatting in bat?
    – postanote
    Aug 6, 2022 at 7:23
  • 1
    Con't... I say this becasue, the more PS commands you try and add to the bat/cmd the more problems you are going to face. Why put yourself through that needed stress?
    – postanote
    Aug 6, 2022 at 7:25
0

As you are writing a batch script, you can use the ^ (as you did before editing the question) for line continuation. Remove the " you added at the beginning of the Powershell command. Add the " just after Write-Host cmdlet as there is a space in your string.

@echo off

title cpm
color e

powershell -command cls; ^
Write-Host "write anything:"; ^
echo "hello"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .