It might be because you have two digits in your displayname. Try changing this:
$content = $content -replace '(DISPLAY\d)',"$primarydisplay"
To this:
$content = $content -replace '(DISPLAY\d{1,2})',"$primarydisplay"
And uncomment this out right below it (remove the #) to see if it worked:
#Write-Output $content
If that worked, then it should write it correctly to the mame.ini file.
If that's not working then double check that it's reading in the mame file by uncommenting the Write-Output line here:
#Read in mame.ini
$content = Get-Content -Path 'E:\Launchbox\Emulators\MAME Lightgun\ini\mame.ini'
#Write-Output $content