PowerShell polyglot

Here's an example of a CMD script that is implemented in PowerShell.

@@:: This prolog allows a PowerShell script to be embedded in a .CMD file.

@@:: Any non-PowerShell content must be preceeded by "@@"

@@setlocal

@@set POWERSHELL_BAT_ARGS=%*

@@if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%

@@PowerShell -Command Invoke-Expression $('$args=@(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^@@'))) & goto :EOF

If you don't need to support quoted arguments, you can even make it a one-liner:

@PowerShell -Command Invoke-Expression $('$args=@(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^@PowerShell.*EOF$')) & goto :EOF