I had the issue of dealing with locked sections and the accepted answer proposes opening up a GUI to solve it, which I am trying to avoid with PowerShell in first place.
Short Answer
Enable Windows Authentication and Disable Anonymous Authentication
$iisSiteName = "Default Web Site"
$iisAppName = "MyApp"
Write-Host Disable anonymous authentication
Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/anonymousAuthentication' -Name 'enabled' -Value 'false' -PSPath 'IIS:\' -Location "$iisSiteName/$iisAppName"
Write-Host Enable windows authentication
Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/windowsAuthentication' -Name 'enabled' -Value 'true' -PSPath 'IIS:\' -Location "$iisSiteName/$iisAppName"
Dealing with Locked Sections
As noted in the IIS documentation:
Authentication sections are usually locked, i.e. they can’t be written
to a web.config file but have to be written to the central
applicationhost.config file instead.
We have to use -PSPath and -Location parameters.
Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location DemoSite/DemoApp