title: Using Powershell to edit/modify a GPO
body: <p>Objective: I'm attempting to modify a GPO (2008R2 AD) via Powershell (v3). Specifically the value of the User Configuration -> Policies -> Windows Settings -> FileRedirection -> Documents UNC path. </p>
  
  <p>Initial attempt:</p>
  
  <pre><code>import-module grouppolicy;
  $StringToFind = "\\this\is\a\template\path";
  $StringToRepalce="\\server123\%CustomerID%\%username%\Documents\";
  $GPOBackupFolder = "C:\src\psh\gpoBackupEditRestore\backups";
  $GPO = copy-gpo -SourceName "Customer GPO Template v1.4" -targetName "Customer $CustomerID" -CopyACL;
  $GPOBackup = $Backup-GPO -guid $gpo.id -path $GPOBackupFolder;
  $GPOBackupXMLPath="$GPOBackupFolder\$($GpoBackup.ID)\Backup.xml";
  $GPOGPReportXMLPath="$GPOBackupFolder\$($GpoBackup.ID)\GPReport.xml";
  $NewBackupXMLPath="$GPOBackupFolder\$($GpoBackup.ID)\nBackup.xml";
  $NewGPReportXMLPath="$GPOBackupFolder\$($GpoBackup.ID)\nGPReport.xml";
  
  $GPOBackup=gc $GPOBackupXMLPath;
  $GPOGPReport= gc $GPOGPReportXMLPath;
  foreach($line in $GPOBackup){ac $NewBackupXMLPath $line.Replace($StringToFind,$StringToReplace);}
  foreach($line in $GPOGPReport){ac $NewGPReportXMLPath $line.Replace($StringToFind,$StringToReplace);}
  
  remove-item -force $GPOBackupXMLPath;
  remove-item -force $GPOGPReportXMLPath;
  move-item -force $NewBackupXMLPath $GPOBackupXMLPath
  move-item -force $NewGPReportXMLPath $GPOGPReportXMLPath
  
  Remove-GPO -ID $GPO.ID   #remove GPO before restore. deleting/commenting this line does not change outcome.
  Restore-GPO -BackupID $GPOBackup.ID -Path $GPOBackupFolder
  </code></pre>
  
  <p>Assuming I'm reading the information correctly at <a href="http://technet.microsoft.com/en-us/library/ee461027.aspx" rel="nofollow">http://technet.microsoft.com/en-us/library/ee461027.aspx</a>, the above Powershell snippit should restore the XML at the local folder location to the GPO in AD.  [[I've confirmed that the template values ($StringToFind) do not occur within any other file in the GPOBackupFolder directory.]]</p>
  
  <p>However, the changed values from the local XML files are NOT being restored to AD. I have confirmed this by doing an additional backup of the GPO after restoring it and comparing the initial (modified) backup files (which have been restored) to the post-restore backup value (now containing the /Original/ values!). </p>
  
  <p>Has anyone else attempted this and/or can explain this behaviour as to why Restore-GPO would not be restoring the content of the backup files?</p>