Converting PowerShell to Batch

by Feb 8, 2018

Here is a fun PowerShell function called Convert-PowerShellToBatch. Provide it with the path to a PowerShell script, or pipe in the results from Get-ChildItem to batch-convert many scripts.

The function creates a batch file per script. When you double-click the batch file, the PowerShell code executes.

function Convert-PowerShellToBatch
        $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
Get-ChildItem -Path C:\path\to\powershell\scripts -Filter *.ps1 |

When you look inside one of the generated batch files, you’ll see that your PowerShell code was converted to a base64-encoded string. So the conversion to batch serves a number of real-world purposes:

  • It may be easier for some to run a PowerShell tool via double-clicking a batch file
  • Inexperienced users are less tempted to fiddle with the code when it is base64-encoded

Disclaimer: Base64-encoding is not an encryption. It is trivial to turn base64-encoded text back into readable plain text. So the technique used here is not at all useful to hide secrets such as passwords.

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at

Twitter This Tip! ReTweet this Tip!