I’m fairly new to PowerShell and I’m not sure how to word my search to find what I’m looking for, so I figured I’d reach out here!
I’m looking to create a batch file or powershell script to change a username in a .txt file for a LAN I’m planning to host, this way all users can have their own name, instead of everyone having the same one or having to search through the file to change it. Some of the users are less than computer savvy, so I want to make it easy.
As an example, I want them to be able to run the script and be prompted to type in a username, replacing “Player1” with their choice of name, such as “nickXIII”
I’ve found a command to replace text, but it just replaces X with Y, both of which are predefined, but I want Y to be a variable string defined by the user when they run the script.
Edit: I figured it out! Is there a way I can streamline this or improve, or does this look good?
$name = Read-Host -Prompt 'Input Username'
powershell -Command "(gc user.txt) -replace 'Player1', '$name' | Set-Content user.txt"
I guess now my question would be, is there a way to change a specific line of text without knowing what that text is to being with? For instance, is a player changes the username from “Player1” to “nickXIII”, when they run the script again to change their name to “JohnnyBoy”, nothing will happen, because “Player1” is not in the file anymore, and that is what the script looks for.