Import-GPO by using folders named after its DisplayName

by JMeterX at 2013-04-22 12:09:07

I am using a powershell script to import GPOs which I have saved as GUIDs (the default).

I wish to import the GPOs on a different system, the GPOs are saved to a CD at this point. So a simple way to import a single GPO would be:


$import_array = get-childitem ($current_drive + $gpo_backup_location) | Select name
foreach ($GPO in $import_array) {
Import-GPO |
-backupgponame $ -path ($Current_Drive + $gpo_backup_location) |
-domain $domain |
-Targetname $ |
-migrationtable ($Current_drive + $mig_table_path) |

the $import_array command gives me my folder names

and so on…


Here is an example of the backup script that I am using to complete this action

Function GPO_Backup_DisplayName ($MenuChoice) {
write ‘Backing Up GPOs by Display Name’
Foreach ($GPO in $GPO_Temp_Backup) {
$GPOBackup = Backup-GPO <br> -name $GPO&#46;DisplayName
-path ($Current_drive + $GPO_Backup_Location) <br> -Domain $domain;<br> Get-GPOReport $GPO&#46;DisplayName
-reporttype HTML <br> -Domain $domain
-path ($Current_drive + $GPO_Backup_HTML_Report + $GPO.DisplayName + “.html”);
-path ($Current_drive + $GPO_Backup_Location + “{” + $GPOBackup.Id + “}”) <br> -newname $GPO&#46;DisplayName<br> }<br>}</code><br><br>This script saves all the GPO's as their DisplayName instead of the ID it is typically saved by. I want to be able to run an import-gpo block of code such as the one at the very beginning that will grab each one of those GPOs and import them. The issue I am having is it cannot find the names of the folders. I tried the code for the import (at the very top of this post) where my $import_array gives me the folder names (example shown below) but I cannot get it to run through this folder list. I did not want to have to use text files but I might have too.<br><br><code>Name<br>----------<br>GPO-1<br>GPO-2<br>GPO-3<br>GPO-4<br>and so on&#46;&#46;&#46;</code></blockquote>by poshoholic at 2013-04-23 21:06:53<blockquote>Could you rephrase the question? I'm not sure what folder name you are talking about.</blockquote>by JMeterX at 2013-04-29 06:14:48<blockquote>I have edited the question</blockquote>by poshoholic at 2013-04-29 06:42:51<blockquote>Let's try a few things to help work this problem out.<br><br>First, when you are import an array, unless you are using PowerShell 3 you should explicitly make sure that is an array (see <!-- m --><a class="postlink" href=""> ... -an-array/</a><!-- m -->). Also, if all you want from the imported contents of the file is the Name parameter, then you should use Select-Object -ExpandProperty instead of Select-Object with the Property parameter. This forces PowerShell to pull the property from the object rather than return an object with one property.<br><br>Put those two together, and that means this line:<br><code>$import_array = get-childitem &#40;$current_drive + $gpo_backup_location&#41; | Select name</code><br>Should be written like this:<br><code>$import_array = @&#40;get-childitem &#40;$current_drive + $gpo_backup_location&#41; | Select-Object -ExpandProperty name&#41;</code><br>Now, with that line you are guaranteed in PowerShell 2 or 3 to have an array of strings (which may be empty if you don't have any files in the folder you specify).<br><br>Once that is done, let's look inside of your foreach loop. In order to make debugging easier, it can help to put the parameters together as a collection so that you can output them, make sure that they are what you think they should be. Splatting helps with this. To leverage splatting, plus the change we made above which makes the $GPO variable be a string instead of an object with a name parameter, we can change this (which I think you typed wrong because the syntax with the pipelines is incorrect):<br><code>Import-GPO |<br>-backupgponame $ -path &#40;$Current_Drive + $gpo_backup_location&#41; |<br>-domain $domain | <br>-Targetname $ |<br>-migrationtable &#40;$Current_drive + $mig_table_path&#41; |<br>-Createifneeded;</code><br>to this:<br><code>$importGPOParameters = @{<br> BackupGpoName = $GPO<br> Path = $Current_Drive + $gpo_backup_location<br> Domain = $domain<br> TargetName = $GPO<br> MigrationTable = $Current_drive + $mig_table_path<br> CreateIfNeeded = $true<br>&#41;<br>Import-GPO @importGPOParameters</code><br>One nice thing about that approach is that you can see the parameter values before they go into Import-GPO when debugging, and you could optionally output them using one of the Write-* commands if you wanted some visual cue as to what parameters were going into your Import-GPO command. I strongly recommend you do this. Adding more Write-* commands to your scripts (Write-Verbose and Write-Debug are helpful here because they are not shown by default) really helps when troubleshooting. Also, don't be afraid to use the debugger (either in PowerShell ISE or one of the other editors that are available). Both really help to see what is going on. For PowerShell 2, I recommend PowerGUI or PowerSE, both of which are free. If you are in PowerShell 3 though, PowerShell ISE is definitely the way to go.<br><br>Give some of those changes a try and see if brings you any closer to solving your problem. If not, if you have errors share those errors here. Or if you're stuck, share script up to the point where you are stuck, and I can suggest additional debugging tips to help you work it out.</blockquote>by JMeterX at 2013-04-29 08:26:21<blockquote>Thanks for the quick reply. I am pretty new to Powershell so I am still wrapping my head around it. One thing I don't understand is why when I use a &quot; | &quot; to separate my lines it does not work, but if I use a &quot; " it does work. I have seen many times people say not to use the tick mark but I can’t seem to get anything else to actually work. But that’s another problem for a different day. I attempted some of your write outs, most of this is on a system in which I cannot copy and paste to the forum so pasting error codes will not be helpful. However, I see your point with the arrays, I sorta questioned if I was really doing that correctly or not. Though, both ways were storing the same results. I also had at one point the select-object in my array phase but didn’t think there would be a difference to make it select name. I see your point on this, thanks! At any rate, I did change my code to:

Function GPO_Import_W/MigrationTable ($MenuChoice) {
write ‘Importing GPOs with Migration Table’
$import_array = @(get-childitem ($Current_drive + $GPO_Backup_Location) + Select-Object -ExpandProperty name)
write $import-array
Foreach ($GPO2 in $import_array) {
$importgpoparameters = @(
Import-GPO <br> -BackupGPOName $GPO&#46;name -path ($Current_drive + $GPO_Backup_Location)
-Domain $domain <br> -TargetName $GPO&#46;name
-MigrationTable ($Current_drive + $Mig_Table_Path) `
write $importparamters
import-gpo @importgpoparameters

The code does function but it never gets past -backupgponame and -path . Instead I receive an error about the GPO not being able to import because no backup that is named “GPO-1” was found at This is the original error I have been receiving with my code prior to the changes. It simply cannot look at the folder names and import that GPO. This is where I am stuck…Not sure if its even fundamentally possible. But to move on, following the error, I receive a prompt to input the ID of the GPO in which I want to import followed by its path. I never get to the point where I can investigate the write $importgpoparamters section
by JMeterX at 2013-04-29 08:51:32
I am guessing that it will not work because it inherently looks for the file to be saved as its ID name and since I am saving them by their displayname it will never find the correct place.
by poshoholic at 2013-04-30 06:06:30
That could definitely be possible. If you are saving the files using the display name, then trying later look them up by name, that would only work if display name and name were one and the same. Based on that comment I wasn’t sure if you needed more help right now or not. If you have additional questions, please let me know.