Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, April 20, 2011

Modify the Internet Explorer Compatibility List with PowerShell

Here is a good, real world question.  While examining the Group Policies settings for Internet Explorer in class.  The question came about as to whether or not the list can be modified in Group Policy by using PowerShell.  Well, yes you can.

The first thing is to determine what registry key we need to modify.  For the Compatibility list, the key is:
“HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\BrowserEmulation\PolicyList”

To utilize PowerShell for this task, we first need to add the Group Policy Module.
Import-Module GroupPolicy

Next, to add the website names to the GPO named IE Settings, type:
Set-GPRegistryValue –Name “IE Settings” –Key “HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\BrowserEmulation\PolicyList” –ValueName “” –Type String –Value “”

Opening the GPO in the Group Policy Management Editor and manually entering the web site would be faster for a single site.  But what if you have a whole list?  That is where we need to turn to a PowerShell script.

The PowerShell code below will allow you to populate the contents of a text file in the Internet Explorer 7 Compatibility list in the GPO of your choice.  This will remove any entries already in the GPO.  Each time it is ran, it purges the compatibility list and recreates it.  For that reason, use the text file as your master list for websites that need to be ran in compatibility mode.

To use this code, copy and paste it into your PowerShell ISE.  Change the file location for the text file and the name of the GPO and run it.

Script Name: ManageCompatabilityList.ps1
Version: 1.0
Author: Jason A. Yoder, MCT


Script Purpose:
This script will add websites to the Internet Explorer 7
Compatibility List in a Group Policy.  The website address
will come from a text file.  The text file will completely
manage what is contained in the Compatibility list GPO. If
a website is removed from the text file, or is not in the
it will also be removed from the GPO.

This script has only been tested on a Windows Server 2008 R2
domain controller.

Revision History:
Created: January 28, 2011
Status: Completed

# ===========================================================
# Global Variables

$Website_List = "E:\Compatibility List\Websites.txt"
# file and location of the text file holding the list of
# websites that need to be included in the Compatibility
# list of the GPO.

$GPOName = "IE Settings"
# This is the name of the Group Policy that contains the
# Compatibility List to be modified.

$ListKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\BrowserEmulation\PolicyList"
# The registry key to be modified. 

# == End of Global Variables ================================

# ===========================================================
# Main Code:

Import-Module GroupPolicy
# Imports the GroupPolicy cmdlets for PowerShell into this
# session.

$ErrorActionPreference = "SilentlyContinue"
# Should the Compatibility list inside the GPO be not
# configured, an error would be generated.  This allows
# the script to continue execution.

# Remove the current contents of the Compatibility List.
Get-GPRegistryValue -Name $GPOName -Key $ListKey | `
Remove-GPRegistryValue -Name $GPOName -Key $ListKey

# Load the list of websites into memory.
$Websites = Get-Content $Website_List

# Add each of the websites to the GPO
For ($i = 0; $i -le $Websites.count-1; $i++)
    Set-GPRegistryValue -Name $GPOName `
    -Key $ListKey -ValueName $Websites[$i] `
    -Type String -Value $Websites[$i]
$ErrorActionPreference = "Stop"
#Reset the error action mode of PowerShell.

# == End of Main Code =======================================

No comments: