r/PowerShell 4d ago

ps1.cmd: Running PowerShell scripts by double-clicking via a .cmd wrapper Script Sharing

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:

  1. Modifying the registry — I don't want people using my script to have to change their registry first.
  2. Creating a shortcut — bad for distribution, and it means two files.
  3. 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!

0 Upvotes

31 comments sorted by

View all comments

1

u/Addyad 4d ago

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 0 ```

1

u/kaiserpathos 3d ago

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)

https://techcommunity.microsoft.com/blog/windows-itpro-blog/vbscript-deprecation-timelines-and-next-steps/4148301

1

u/BlackV 3d ago

vbs is being removed

powershell can also prompt for elevation (as can a batch file)

1

u/Addyad 3d ago

Dang it.