Garry's Mod

Garry's Mod

Not enough ratings
GMod start.bat modern upgrade to start.ps1 (PowerShell alternative)
By Bloodlessplace
Currently there exists one method accepted by many to startup a dedicated Garry's Mod server, by use of a simple start.bat file.

What this guide will cover is the modern alternative method, which will use PowerShell to startup a server and run it more reliably, while resolving issues previously faced with the currently used method.
   
Award
Favorite
Favorited
Unfavorite
Introduction
Currently start.bat, is the main file that server owners use to configure the parameters of a server such as the IP, port and client port, along with others such as the map, gamemode and max player count.

This of course has worked for the longest time and arguably there is no need for a replacement, as this simply takes care of everything, including its purpose of being used as a watchdog as well to ensure that in the event that the Garry's Mod server crashes and was to close, the start.bat file would restart the server immediately and automatically.

This in theory is what the start.bat is supposed to do, however in reality, it fails to meet this final expectation and will often result in a message being displayed in the command prompt, asking whether you wish for the script to continue execution and will not auto restart the server in many cases as illustrated in the following image.


What this means as a result is that the owner needs to then manually answer Y to continue and restart the server, defeating the purpose of the start script being used as an automatic restart mechanism.

It is as a result hoped that with this guide, adoption for this new method, which will be discussed in this guide using PowerShell will take hold to alleviate the current reliability issues of the current method and provide a fix to allow servers to be automatically restarted without intervention.
Comparison of code
As it currently stands, start.bat files have often used the following or similar code like below.

@echo off cls echo Starting server title srcds.com Watchdog :srcds start /wait srcds.exe -console -game garrysmod -secure -ip 192.168.0.182 -port 27020 +clientport 27003 +map gm_flatgrass_abs_v3c +maxplayers 10 +gamemode sandbox +r_hunkalloclightmaps 0 echo (%time%) WARNING: srcds closed or crashed, restarting. goto srcds

This code is then written into a text file and saved with the .bat extension, allowing the user to execute the newly created windows batch file.

Instead of this, the following code will be used with and run in PowerShell, allowing for a more stable and modern alternative to the current method used.

$arguments = ' -console -game garrysmod -secure -ip 192.168.0.182 -port 27020 +clientport 27003 +map gm_flatgrass_abs_v3c +maxplayers 10 +gamemode sandbox +r_hunkalloclightmaps 0 ' $host.UI.RawUI.WindowTitle = "Gmod Main Server Watchdog" while($true) { Write-output "GMod server starting at: $(Get-Date)" Start-Process "srcds.exe" -ArgumentList $arguments -Wait Write-output "GMod server crashed or shutdown at: $(Get-Date)" }

The only difference with this apart from the layout of the code, is the fact that this code is saved with the .ps1 extension. (this is not a hoax, that is the legit name of the extension for PowerShell scripts)
Creating your new PowerShell start script
Step 1

Start by creating a plain text document.


Step 2

Open your new text document and paste in the PowerShell code shown in the previous code comparison.


Step 3

Make any changes to the configuration that you need to make.

RED marked code can be added/removed/changed at will.

BLUE marked code can be changed to alter the name of the powershell window.


Step 4

Save the new document as "start.ps1" with "All Files" selected in the "Save as type" drop down menu.


Step 5

Copy the new file to you gmod server directory.

Running your PowerShell script
Once you have created your PowerShell script, there will be but one issue, running it.

In order to run it, simply double clicking the file will not work, and instead, you will need to right click and select "Run with PowerShell" to get it started.


Once started you should then get a blue PowerShell window appear with your server starting up shortly afterwards.

Finishing Up
That's it, congratulations, you have now upgraded to use PowerShell.

But what does this mean

What this now means is that if your server crashes and closes, PowerShell will restart it instantly without your intervention, unlike with the previous method, this one works without fail.

The code itself has been put together with ease of use in mind to allow you to easily see and configure the parameters of the server and should help make life easier in that regard too.

Finally, now that you have a script working in PowerShell, you have a greatly expanded capability to code and develop this script to do far more than what is currently possible with Batch files and may mean that other tasks to automate your server can now be developed.
Final Message
I hope that this guide proves to be helpful to current and future server owners. If at all there is any issues with this current guide which you wish for me to change or add to, please do feel free to leave a comment so that i can make any necessary alterations.
10 Comments
sigmagoon69 Sep 3, 2024 @ 8:38am 
you dont need to get your own ip if its for testing, just use localhost. 127.0.0.1
Missing Apr 10, 2023 @ 7:22am 
if you're on windows 11, you must type
"Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser" into your powershell for the script to run!
Woompa Dec 2, 2022 @ 12:03am 
this is amazing... thank you!
Bloodlessplace  [author] Sep 4, 2022 @ 2:31am 
That error appears to be in relation to the "-ip <host machine ip address here>" being set incorrectly.

You will need to run "cmd" from the start menu and then type "ipconfig" and locate the line which says "IPv4 Address, this will be the local address of your host computer your trying to run the server from.

Once you have the local machine ip address, put that in place of whatever is after the "-ip", hopefully that should resolve your problem.
Bloodlessplace  [author] Sep 2, 2022 @ 12:13pm 
Yeah you can remove it entirely
Bloodlessplace  [author] Sep 2, 2022 @ 12:02pm 
@🐱CrazySniperCatt0🐱 Been a while since i've personally played around with the "clientport", however i believe it was used to prevent issues with running multiple servers on one host, but in general for one server this very likely won't be needed at all.
Lord of Owls Sep 2, 2022 @ 9:56am 
shame this is for windows only, the arguments stored in a table was kinda nice
Bloodlessplace  [author] Apr 6, 2020 @ 8:24am 
Would it be possible for you to abbreviate on your question in a bit more detail? If your asking about whether the script will start your Garry's Mod server, then yes, this is intended to be used in that manor, then in the event the Garry's Mod server crashes, PowerShell will automatically restart the Garry's Mod server for you. I hope that helps!
RizzedUrMom Apr 6, 2020 @ 5:49am 
can i start my server threw the power with is as well?
Fesiug Oct 11, 2019 @ 4:33pm 
amazing.