PowerShell: Set Enhanced Protected Mode of Internet Explorer

function changeIeProtectedMode{
    # $hives = 0..4|%{"HKLM:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\$_"}
    $hives = 0..4|%{"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\$_"}
    $keyName='2500' # Key Name '2500' corresponds to 'Protected Mode' in IE
    $keys=$hives|%{Get-ItemProperty -Path $_}|select DisplayName, `
                                                    @{name='status';e={
                                                                    if($_.$keyName -eq 0){'enabled'}
                                                                    elseif($_.$keyName -eq 3){'disabled'}
                                                                    else{'n/a'}                                                                                        
                                                                    }}
    write-host "Original Values are:`r`n$($keys|out-string)" # Key codes 0: Enabled, 3: Disabled 
    #DisplayName      value
    #-----------      -----
    #Computer         n/a
    #Local intranet   enabled
    #Trusted sites    enabled
    #Internet         enabled
    #Restricted sites enabled
    $userResponse = Read-Host 'Enable IE Protected Mode? (yes/no)'
    $intent=switch ($userResponse){
        'no'{3;break}
        'yes'{0;break}
        default{-1}
        }

    #Skipping zone 0 as that is the local machine zone
    if($intent -gt -1){
        $hives[1..4]|%{Set-ItemProperty -Path $_ -Name $keyName -Value $intent}
        $keys=$hives|%{Get-ItemProperty -Path $_}|select DisplayName, `
                                                        @{name='status';e={
                                                                        if($_.$keyName -eq 0){'enabled'}
                                                                        elseif($_.$keyName -eq 3){'disabled'}
                                                                        else{'n/a'}                                                                                        
                                                                        }}
        write-host "New Values are:`r`n$($keys|out-string)"    
        }
    else{write-host 'No changes have been made'}
}
changeIeProtectedMode

Leave a Reply

Your email address will not be published. Required fields are marked *