I have a text file with the following content:
Static Text MachineA MachineB MachineC
Just Another Line
The first line has two static words (Static Text) with a space between. After those two words there are 0 or more computer names, also seperated with a space.
I need to find a way to add text to the first line (second line does not change) if there are 0 computers but also if there is 1 or more computers. I need to replace all computer names with a new computer name. So the script should edit the file to get something like this:
Static Text MachineX MachineY
Just Another Line
I've looked at the -replace function with Regex but can't figure out why it is not working. This is the script I have:
$OptionsFile = "C:\scripts\OptionsFile.txt"
$NewComputers = "MachineX MachineY"
$content = Get-Content $OptionsFile
$content |
ForEach-Object {
if ($_.ReadCount -eq 1) {
$_ -replace '\w+', $NewComputers
} else {
$_
}
} |
Set-Content $OptionsFile
I hope someone can help me out with this.