
Ingo Karsteinの素晴らしいスクリプトを拡張し、GUIサポートを追加しました。GUI出力と入力は1つのスイッチで有効になり、実際のWindows実行可能ファイルが生成されます。PowerShell 5.x互換のスクリプトのみをコンパイルします。オプションのグラフィカルフロントエンドWin-PS2EXEも利用可能です。
モジュールバージョン。
スクリプトベースのバージョンはこちら(https://github.com/MScholtes/TechNet-Gallery)にあります。
著者: Markus Scholtes
バージョン: 1.0.18
日付: 2026-06-06
PS C:\> Install-Module ps2exe
またはこちらからダウンロード: https://www.powershellgallery.com/packages/ps2exe/。
Invoke-ps2exe .\source.ps1 .\target.exe
または
ps2exe .\source.ps1 .\target.exe
"source.ps1" を実行可能ファイル target.exe にコンパイルします(".\target.exe" を省略すると、出力は ".\source.exe" に書き込まれます)。
またはグラフィカルフロントエンドを起動するには、次のように入力します。
Win-PS2EXE
ps2exe [-inputFile] '<file_name>' [[-outputFile] '<file_name>']
[-prepareDebug] [-x86|-x64] [-lcid <id>] [-STA|-MTA] [-noConsole] [-conHost] [-UNICODEEncoding]
[-credentialGUI] [-iconFile '<filename>'] [-$embedFiles <hashtable>] [-title '<title>'] [-description '<description>']
[-company '<company>'] [-product '<product>'] [-copyright '<copyright>'] [-trademark '<trademark>']
[-version '<version>'] [-configFile] [-noOutput] [-noError] [-noVisualStyles] [-exitOnCancel]
[-DPIAware] [-requireAdmin] [-supportOS] [-virtualize] [-longPaths]
inputFile = Powershell script that you want to convert to executable (file has to be UTF8 or UTF16 encoded)
outputFile = destination executable file name or folder, defaults to inputFile with extension '.exe'
prepareDebug = create helpful information for debugging
x86 or x64 = compile for 32-bit or 64-bit runtime only
lcid = location ID for the compiled executable. Current user culture if not specified
STA or MTA = 'Single Thread Apartment' or 'Multi Thread Apartment' mode
noConsole = the resulting executable will be a Windows Forms app without a console window
conHost = force start with conhost as console instead of Windows Terminal (disables redirections)
UNICODEEncoding = encode output as UNICODE in console mode
credentialGUI = use GUI for prompting credentials in console mode
iconFile = icon file name for the compiled executable
embedFiles = files to embed given as hash, will be extracted to key of hash, source file names must be unique
(e.g. -embedFiles @{'Targetfilepath'='Sourcefilepath'} )
title = title information (displayed in details tab of Windows Explorer's properties dialog)
description = description information (not displayed, but embedded in executable)
company = company information (not displayed, but embedded in executable)
product = product information (displayed in details tab of Windows Explorer's properties dialog)
copyright = copyright information (displayed in details tab of Windows Explorer's properties dialog)
trademark = trademark information (displayed in details tab of Windows Explorer's properties dialog)
version = version information (displayed in details tab of Windows Explorer's properties dialog)
configFile = write config file (<outputfile>.exe.config)
noOutput = the resulting executable will generate no standard output (includes verbose and information channel)
noError = the resulting executable will generate no error output (includes warning and debug channel)
noVisualStyles = disable visual styles for a generated windows GUI application (only with -noConsole)
exitOnCancel = exits program when Cancel or "X" is selected in a Read-Host input box (only with -noConsole)
DPIAware = if display scaling is activated, GUI controls will be scaled if possible (only with -noConsole)
requireAdmin = if UAC is enabled, compiled executable run only in elevated context (UAC dialog appears if required)
supportOS = use functions of newest Windows versions (execute [Environment]::OSVersion to see the difference)
virtualize = application virtualization is activated (forcing x86 runtime)
longPaths = enable long paths ( > 260 characters) if enabled on OS (works only with Windows 10)
生成された実行可能ファイルには、次の予約パラメーターがあります。
-? [<MODIFIER>] Powershell help text of the script inside the executable. The optional parameter combination
"-? -detailed", "-? -examples" or "-? -full" can be used to get the appropriate help text.
-debug Forces the executable to be debugged. It calls "System.Diagnostics.Debugger.Launch()".
-extract:<FILENAME> Extracts the powerShell script inside the executable and saves it as FILENAME.
The script will not be executed.
-wait At the end of the script execution it writes "Hit any key to exit..." and waits for a key to be pressed.
-end All following options will be passed to the script inside the executable.
All preceding options are used by the executable itself and will not be passed to the script.
PS2EXEはPowerShell Coreでも使用できます。そのためには、上記のようにPowerShell CoreにPS2EXEモジュールをインストールします。しかし、.Net Coreにはコンパイラが付属していないため、.Net Frameworkのコンパイラが使用されます(Windowsには.Net FrameworkとPowerShell 5.1が含まれています)。
このため、PS2EXEはPowerShell 5.1互換のスクリプトのみをコンパイルし、.Net 4.xバイナリを生成しますが、依存関係なしにサポートされているすべてのWindows OSで直接使用できます。
-embedFiles パラメーターにファイルパスのハッシュテーブルを指定すると、それらのファイルがコンパイルされた実行可能ファイルに埋め込まれます。 実行可能ファイルの起動時に、それらのファイルは指定されたパスにディスクに書き込まれます。例: -embedFiles @{'Targetfilepath1'='Sourcefilepath1';'Targetfilepath2'='Sourcefilepath2'}。 ソースファイル名は一意でなければなりません。絶対パスと相対パスの両方が使用できます。ターゲットパスが '.\' で始まる相対パスの場合、実行可能ファイルからの相対パスとして解釈され、先頭の '.\' がない場合は実行時の現在のパスからの相対パスとして解釈されます。 必要に応じて、起動時にディレクトリが自動的に作成されます。ターゲットパスでは、%TEMP% や %APPDATA% のようなcmd.exe形式の環境変数が実行時に展開されます。 埋め込まれたファイルの作成に失敗すると、コンパイルされた実行可能ファイルの実行は直ちに停止します。
基本的な入出力コマンドはPS2EXE用にC#で書き直す必要がありました。コンソールモードの Write-Progress(作業量が多すぎる)および Start-Transcript/Stop-Transcript(Microsoftによる適切なリファレンス実装がない)は実装されていません。
デフォルトでは、PowerShellのコマンドレットの出力は行ごとにフォーマットされます(文字列の配列として)。コマンドが10行の出力を生成し、GUI出力を使用する場合、それぞれのOK待ちで10個のメッセージボックスが表示されます。これを防ぐには、コマンドを Out-String コマンドレットにパイプします。これにより、出力が10行の文字列配列に変換され、すべての出力が1つのメッセージボックスに表示されます(例: dir C:\ | Out-String)。
PS2EXEは、生成された実行可能ファイルの名前 + ".config" という設定ファイルを作成できます。ほとんどの場合、これらの設定ファイルは不要です。これらは、使用する.Net Frameworkのバージョンを指定するマニフェストです。通常は最新の.Net Frameworkを使用するため、設定ファイルなしで実行可能ファイルを実行してみてください。
コンパイルされたスクリプトは、元のスクリプトと同様にパラメーターを処理します。Windows環境では、すべての実行可能ファイルのパラメーターは型 STRING であるという制限があります。パラメーターの型に暗黙の変換がない場合は、スクリプト内で明示的に変換する必要があります。同じ制限で、実行可能ファイルにコンテンツをパイプすることもできます(パイプされたすべての値は型 STRING です)。
コンパイルされたスクリプトにパスワードを保存しないでください! -extract パラメーターを使用すると、簡単にスクリプトを逆コンパイルできます。例えば
Output.exe -extract:C:\Output.ps1
は、Output.exe に保存されているスクリプトを逆コンパイルします。また、スクリプトは(意図的に)実行可能ファイル内に平文で保存されていることに注意してください!
PS2EXEはスクリプトを実行可能ファイルに変換するため、スクリプト関連の変数は使用できなくなります。変数 $MyInvocation はスクリプトとは異なる値に設定されます。 特に変数 $PSScriptRoot は空です - 代わりに $ScriptRoot を使用できます。 次のコード行を使用すると、コンパイルされているかどうかに関係なく $PSScriptRoot を取得できます。
if (!$PSScriptRoot) { $PSScriptRoot = $ScriptRoot }
-noConsole モードのスクリプトで外部ウィンドウが開かれると(Get-Credential や cmd.exe シェルを必要とするコマンドなど)、次のウィンドウがバックグラウンドで開かれます。 理由は、外部ウィンドウを閉じるときに、Windowsが親ウィンドウをアクティブにしようとするためです。コンパイルされたスクリプトにはウィンドウがないため、代わりにコンパイルされたスクリプトの親ウィンドウ(通常はExplorerやPowerShellのウィンドウ)がアクティブになります。 回避策として、$Host.UI.RawUI.FlushInputBuffer() はアクティブ化できる非表示ウィンドウを開きます。次の $Host.UI.RawUI.FlushInputBuffer() の呼び出しでこのウィンドウが閉じられます(以下同様)。 次の例では、"ipconfig | Out-String" の単一呼び出しのようにバックグラウンドでウィンドウが開かれることはありません。
$Host.UI.RawUI.FlushInputBuffer()
ipconfig | Out-String
$Host.UI.RawUI.FlushInputBuffer()