Posted it also here:
[To see links please register here]
Following this thread: <br>
[To see links please register here]
<br>
<br>
<br>
**you can convert any PowerShell script into a batch file easily using this PowerShell function:**
```
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
```
<br>To convert **all** PowerShell scripts inside a directory, simply run the following command:<br>
```
Get-ChildItem -Path <DIR-PATH> -Filter *.ps1 |
Convert-PowerShellToBatch
```
Where <DIR-PATH> is the path to the desired folder. For instance: <br>
```
Get-ChildItem -Path "C:\path\to\powershell\scripts" -Filter *.ps1 |
Convert-PowerShellToBatch
```
<br>To convert a **single** PowerShell script, simply run this:
<br>
```
Get-ChildItem -Path <FILE-PATH> |
Convert-PowerShellToBatch
```
Where <FILE-PATH> is the path to the desired file.
<br>
<br>
The converted files are located in the source directory. i.e., *<FILE-PATH<FILE-PATH>>* or *<DIR-PATH<DIR-PATH>>*.
<br><br>
**Putting it all together:**<br>
create a .ps1 file (PowerShell script) with the following code in it:<br>
```
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
# change <DIR> to the path of the folder in which the desired powershell scripts are.
# the converted files will be created in the destination path location (in <DIR>).
Get-ChildItem -Path <DIR> -Filter *.ps1 |
Convert-PowerShellToBatch
```
<br>And don't forget, if you wanna convert only one file instead of many, you can replace the following
```
Get-ChildItem -Path <DIR> -Filter *.ps1 |
Convert-PowerShellToBatch
```
with this:
```
Get-ChildItem -Path <FILE-PATH> |
Convert-PowerShellToBatch
```
as I explained before.