$Documents = [Environment]::GetFolderPath("MyDocuments")
Copy-Item -Path "$InstallDirectory\ProfileTemplate" -Destination "$Documents\My Games\Age of Mythology" -Recurse
$Display = Get-PrimaryDisplay
Write-ReplaceContentInFile -Pattern '^xres=(.+)' -Substitution "xres=$($Display.Bounds.Width)" -FilePath "$InstallDirectory\startup\user.cfg"
Write-ReplaceContentInFile -Pattern '^yres=(.+)' -Substitution "yres=$($Display.Bounds.Height)" -FilePath "$InstallDirectory\startup\user.cfg"
Write-ReplaceContentInFile -Pattern '^xres (.+)' -Substitution "xres $($Display.Bounds.Width)" -FilePath "$Documents\My Games\Age of Mythology\startup\persistent.cfg"
Write-ReplaceContentInFile -Pattern '^yres (.+)' -Substitution "yres $($Display.Bounds.Height)" -FilePath "$Documents\My Games\Age of Mythology\startup\persistent.cfg"
New-Item -Path "registry::\HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age of Mythology Expansion Pack" -Force
New-Item -Path "registry::\HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age of Mythology Expansion Pack\1.0" -Force
New-ItemProperty -Path "registry::\HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age of Mythology Expansion Pack\1.0" -Name "FIRSTRUN" -Value 1 -Force
New-ItemProperty -Path "registry::\HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age of Mythology Expansion Pack\1.0" -Name "SystemInitialization" -Value "1" -Force
$DocumentsPath = [Environment]::GetFolderPath("MyDocuments")
Remove-Item -Path "$DocumentsPath\My Games\Age of Mythology" -Force -Recurse
Remove-Item -Path "registry::\HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age of Mythology Expansion Pack" -Force -Recurse
# Sanitize name as it will be used in a file name
$NewPlayerAlias = $NewPlayerAlias.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$Guid = [Guid]::NewGuid().ToString().ToUpper()
$Documents = [Environment]::GetFolderPath("MyDocuments")
Copy-Item -Path "$InstallDirectory\ProfileTemplate\Users\Default.prf" -Destination "$Documents\My Games\Age of Mythology\Users\$NewPlayerAlias.prf"
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Pattern '<profilename>(.*)</profilename>' -Substitution "<profilename>$NewPlayerAlias</profilename>" -FilePath "$Documents\My Games\Age of Mythology\Users\$NewPlayerAlias.prf"
Write-ReplaceContentInFile -Pattern '<profilelanname>(.*)</profilelanname>' -Substitution "<profilelanname>$NewPlayerAlias</profilelanname>" -FilePath "$Documents\My Games\Age of Mythology\Users\$NewPlayerAlias.prf"
Write-ReplaceContentInFile -Pattern '<profileguid>(.*)</profileguid>' -Substitution "<profileguid>{$Guid}</profileguid>" -FilePath "$Documents\My Games\Age of Mythology\Users\$NewPlayerAlias.prf"
Write-ReplaceContentInFile -Pattern '^playerProfileName "(.+)"' -Substitution "playerProfileName ""$NewPlayerAlias""" -FilePath "$Documents\My Games\Age of Mythology\startup\persistent.cfg"
Write-ReplaceContentInFile -Pattern '^playerGUID "(.+)"' -Substitution "playerGUID ""{$Guid}""" -FilePath "$Documents\My Games\Age of Mythology\startup\persistent.cfg"
$NewPlayerAlias | Out-File -FilePath "$Documents\My Games\Age of Mythology\Users\LastProfile.txt"