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.-Command
Invoke-Command
icm
[scriptblock]::Create()
3. Code
If the script encoding is different, set Get-Content
the 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