How to execute Powershell commands from a batch file? multiline comment

Call powershell multi-line comment method in a single bat

1. source

How to execute powershell commands from a batch file?

kapitanrum's answer: https://stackoverflow.com/a/41986771

2. Idea

Comments in batrem use or ::, while powershell single-line comments use #, multi-line comments <##>, this difference can easily convert ps1 scripts to bat suffix scripts without a lot of changes.

To run a powershell command in a bat script , you need to use the parameters of powershell.exe . Luckily, in ps1 you can use (alias ) to run scriptblocks , so we just need to pass the script text through and do a simple conversion.-CommandInvoke-Commandicm[scriptblock]::Create()

3. Code

If the script encoding is different, set Get-Contentthe encoding when reading, or [IO.File]read the script with the class specified encoding format.

<# :
@powershell "icm ([scriptblock]::Create((gc '%~f0' -Raw -Encoding UTF8)))"
exit
#>

Write-Host "Hello World" -fore Red
pause
# powershell script

Guess you like

Origin blog.csdn.net/qq_41755979/article/details/107370596