Skip to content

Instantly share code, notes, and snippets.

@earthdiver
Last active April 29, 2024 14:27
Show Gist options
  • Select an option

  • Save earthdiver/f7230330268b2fee6978bd1201a3d6a1 to your computer and use it in GitHub Desktop.

Select an option

Save earthdiver/f7230330268b2fee6978bd1201a3d6a1 to your computer and use it in GitHub Desktop.
One-line preambles to convert a PowerShell script into a batch file.
@Powershell -NoP -C "$input|&([ScriptBlock]::Create((gc '%~f0'|?{$_.ReadCount -gt 1}|Out-String)))" %* & pause & exit/b
@Powershell -NoP -C "$PSCommandPath='%~f0';$PSScriptRoot='%~dp0'.TrimEnd('\');$input|&([ScriptBlock]::Create((gc '%~f0'|?{$_.ReadCount -gt 1}|Out-String)))" %* & pause & exit/b
@Powershell -NoP -C "$input|.([ScriptBlock]::Create('$MyInvocation.MyCommand|Add-Member -M NoteProperty -Na Path -V ''%~f0'';$PSCommandPath=''%~f0'';$PSScriptRoot=''%~dp0''.TrimEnd(''\'');'+(gc '%~f0'|?{$_.ReadCount -gt 1}|Out-String)))" %* & pause & exit/b
@setlocal EnableDelayedExpansion&set a=%*&(if defined a set a=!a:"=\"!)&Powershell -NoP -C "$PSCommandPath='%~f0';$PSScriptRoot='%~dp0'.TrimEnd('\');$input|.([ScriptBlock]::Create(((gc '%~f0'|?{$_.ReadCount -gt 1}|Out-String)-replace'(?is)(^^(?:(?(o)(?:(?^!<#|@[''\""])[^^\s#''\""()])+|(?^!))|\s+|#[^^\n]*\n|<#(?:[^^#]|#(?^!>))*#>|''(?:[^^''`]|`.)*''|\""(?:[^^\""`]|`.)*\""|@''(?:(?^!\n''@).)*\n''@|@\""(?:(?^!\n\""@).)*\n\""@|\[(?:Alias|CmdletBinding)[^^\]]*\]|\[OutputType\((?>(?:[^^()]+|(?<o>\()|(?<-o>\)))*)(?(o)(?^!))\)\]|(?<p>Param)|(?(p)(?:(?<o>\()|(?<-o>\))(?(o)|(?<-p>)))|(?^!))|(?<b>Begin)|(?(b)\{|(?^!)))*)(.*)$','$1;$MyInvocation.MyCommand|Add-Member -M NoteProperty -Na Path -V ''%~f0'';$PSCommandPath=''%~f0'';$PSScriptRoot=''%~dp0''.TrimEnd(''\'');$2'))) !a!"&pause&exit/b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment