r/PowerShell • u/TanixLu • Aug 04 '26
Script Sharing ps1.cmd: Running PowerShell scripts by double-clicking via a .cmd wrapper
I'm a Left 4 Dead 2 player, and recently I ran into a small problem: the server I like to play on is often completely full. It occurred to me that I could write a script to poll the server and check whether a slot had opened up. After some research, I found that cmd scripts have a hard time handling network requests, while PowerShell is powerful enough to handle the job easily. Automatically checking the player count and joining the server is a pretty simple task, so I had an AI write it for me. The result was great — fully functional. All I had to do was copy the server address, then right-click the .ps1 script and choose "Run with PowerShell."
The only thing was, having to right-click to run it felt awkward, and it doesn't match most people's intuition about computers. If I have a script or an app, I should be able to double-click to run it! So I started looking for ways to run a PowerShell script by double-clicking. There are indeed a few, but none of them are very elegant:
Modifying the registry — I don't want people using my script to have to change their registry first.
Creating a shortcut — bad for distribution, and it means two files.
Creating a cmd launcher — I prefer the simplicity of a single file, and a single file is also easier to distribute.
In the end, I found my ideal solution in a Stack Overflow answer: stuff the PowerShell script inside a cmd file! So I open-sourced the ps1.cmd project. My final implementation isn't exactly the same as that answer — you can check out the GitHub page for the details.
The goal of ps1.cmd is maximum compatibility: it should work with any PowerShell script and behave identically when executed. I hope this project helps you out, and if you run into any problems, feel free to open an issue!
16
u/T__W__T Aug 04 '26
From security perspective you should use 2 files (a ps1 and cmd which calls the ps1). Security > Elegance.
3
2
u/VirtualDenzel Aug 05 '26
From a security perspective you compile it to an exe and sign it. And block cmd/ps on the workstation.
3
u/T__W__T Aug 05 '26
I wouldn't touch a 3rd party script to binary compiler.
0
u/VirtualDenzel Aug 19 '26
You don't need third party. Ai can make an oss inhouse variant in just 1-2 prompts.
3
8
u/Gormless_Shrimp_635 Aug 04 '26
You could use the PowerShell Pro Tools extension in VS Code to compile it to an .exe
2
u/BlueLining-Solutions Aug 06 '26
Why bit then just convert the powershell script to an exe? Might need to add a couple security adjustments or imbed security cert in the script but normally works
3
u/g3n3 Aug 04 '26
https://gist.github.com/Jaykul/4b4610142cc20bb55f1616cbcf8e9574 is the nicest format I’ve seen
3
u/society_victim Aug 04 '26
Ps2exe ?
2
u/Maeldruin_ Aug 04 '26
Not sure why this got downvoted. Ps2Exe is by far the easiest way to make a script a one and done solution.
4
u/BlackV Aug 04 '26
Maeldruin_
Not sure why this got downvoted. Ps2Exe is by far the easiest way to make a script a one and done solution.I would say
because its hiding the code
because it gets flagged by av systems
because you are now running some random exe on a user machine
1
u/Maeldruin_ Aug 04 '26
Those are good reasons if this was being used in a business environment. This is pretty clearly a personal use case though.
1
u/BlackV Aug 04 '26 edited Aug 04 '26
They are releasing this to the public (I believe they mentioned something like that earlier)
Edit: I I think this was itI don't want people using my script to have to change their registry first.
I disagree, nothing there is business specifically, just good general hygiene overall
Although I also don't like the hybrid batch/ps1 either but it's readable and validated at least
1
1
u/Addyad Aug 04 '26
I usually use a vbs script. It bypasses the execution policy. And asks to elevate if admin rights is necessary. Since the psscript location is given, you can keep this vbs script in desktop and just double click.
' Create shell and filesystem objects
Set shell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
' Specify the full path to your PowerShell script
' UPDATE THIS PATH with the complete path to your .ps1 file
' Example: psScriptPath = "C:\Scripts\MyPowerShellScript.ps1"
psScriptPath = "C:\Scripts\MyPowerShellScript.ps1"
' Check if the PowerShell script exists
If Not fso.FileExists(psScriptPath) Then
MsgBox "PowerShell script not found at: " & psScriptPath, vbCritical, "Error - File Not Found"
WScript.Quit 1
End If
' Launch PowerShell script with administrator privileges
' Parameters: NoProfile (skip profile loading), Bypass (ignore execution policy)
' runas (elevate to admin), 1 (show window, use 0 to hide)
shell.ShellExecute "powershell.exe", _
"-NoProfile -ExecutionPolicy Bypass -File """ & psScriptPath & """", _
"", _
"runas", _
1
' Check if execution failed
If Err.Number <> 0 Then
MsgBox "Failed to execute PowerShell script. Error: " & Err.Description, vbCritical, "Execution Error"
WScript.Quit 1
End If
' Exit with success code
WScript.Quit 01
u/kaiserpathos Aug 04 '26
Same, but unfortunately we're headed to retirement of vbs in Win11 as well (though pretty sure it can be re-enabled by the time we hit 2027)
1
u/BlackV Aug 04 '26
vbs is being removed
powershell can also prompt for elevation (as can a batch file)
1
-4
u/Shayden-Froida Aug 04 '26
Look into having the powershell script create a desktop shortcut for itself. I recommend an AI prompt such as "Add a -Install flag to the command line that will install a desktop shortcut called _____ and exit. If the shortcut exists, update it to make sure it points to the current script."
8
u/Creative-Type9411 Aug 04 '26
Put this at the top of your script and you can either rename it to PS1 or CMD and run it...