Empty ADGroup

Script reads old group name and new group name from CSV file.  The processes data from XML and PS1 file. It exports the previous rights to a CSV file in the subdir CSVfiles. Needs XML and CSV files.

PS1:

$xmlConfigfile = ".EmptyADgroup.xml"

	While (((Test-Path $xmlConfigfile) -eq $false) -or ($NoXML)){
		[System.Windows.Forms.MessageBox]::Show("ERROR: $xmlConfigfile not found!")
	write-host De XML file kan niet gevonden worden -F Red
	If (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)}
	exit
	}

	If (-not ($CSV -and $Header -and $Migrated -and $Domain -and $OUlocal1 - $OUlocal2 -and $OUGlobal1 -and $OUGlobal2)) {
		$xml = get-content $xmlConfigfile
		If (-not $CSV) {$CSV = $xml.Config.Settings.CSV}
		If (-not $Header) {$Header = $xml.Config.Settings.Header}
		If (-not $Migrated) {$Migrated = $xml.Config.Settings.Migrated}
		}

Import-Module ActiveDirectory

		While (((Test-Path $CSV) -eq $false) -or ($NoCSV)){
		[System.Windows.Forms.MessageBox]::Show("ERROR: $xmlConfigfile not found!")
	write-host De CSV file kan niet gevonden worden -F Red
	If (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)}
	exit
	}

$list = @(import-csv -Delimiter ';' $CSV)
write-host ".CSV file contains" $list.count " lines." -F Yellow -B DarkCyan
$list[0]

if ($error.count -ne 0)

{
	write-host "An error occurred during the operation. Details follow:"
	$error[0].categoryInfo
	$error[0].invocationinfo
	write-host "=========================================================="
	write-host "Quit due to an error" -Fore Red
	Exit
}
else
{
	#"Successfully opened .CSV file..."
}

#Loop through .CSV file
foreach($entry in $list)

{
	# Reset the variable to make sure that they are clean before processing a user.
	$Oldgroup=$entry.OldGroup
	$NewGroup=$entry.NewGroup

	if ($Oldgroup -ne $null){$CSVExportFile = ($Oldgroup+".csv")}

		While (((Test-Path ".CSVFiles$CSVExportFile") -ne $false) -or ($NoCSVExportFile)){
		[System.Windows.Forms.MessageBox]::Show("ERROR: $CSVExportFile already exists!")
	write-host Het CSV bestande $CSVExportFile bestaat al -F Red
	If (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)}
	exit
	}
	write-host "Er wordt een export gemaakt van de groep $Oldgroup" -b DarkCyan -f Yellow
	$lijst = get-adgroupmember	 $Oldgroup -recursive

	Add-content -Value $Header -Path ".CSVFiles$CSVExportFile"

	foreach ($item in $lijst){
	$Outinfo = $Oldgroup + ";" + $item.samaccountname
	Add-content -Value $Outinfo -Path ".CSVFiles$CSVExportFile"}

			While (((Test-Path ".CSVFiles$CSVExportFile") -eq $false) -or ($NoCSVExportFile)){
		[System.Windows.Forms.MessageBox]::Show("ERROR: $CSVExportFile not found!")
	write-host Het CSV bestand $CSVExportFile is niet weggeschreven -F Red
	If (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)}
	exit
	}
	write-host "De export is gemaakt van de groep $Oldgroup op locatie .CSVFiles$CSVExportFile" -b DarkCyan -f Yellow
	write-host "De users in de groep $Oldgroup worden nu uit de groep gehaald" -b DarkCyan -f Yellow
	foreach ($item in $lijst){

	Remove-ADGroupMember $oldgroup -Members $item.samaccountname -Confirm:$false
	}

	$lijst = get-adgroupmember $Oldgroup -recursive
	if ($lijst -ne $null) {write-host "Niet alle users zijn uit de groep $oldgroup gehaald" -b Black -f Red}
	if ($lijst -eq $null) {write-host "Alle users zijn uit de groep $oldgroup gehaald" -b DarkCyan -f Yellow}
	if ($lijst -eq $null) {$Description = get-adgroup "ALC_APL_mibores" -Properties * | ForEach-Object {$_.Description}}
	$AddDescription = "$Migrated $Newgroup |"
	$Description = [string]$description
	$Description = ($AddDescription+$description)
	if ($lijst -eq $null) {Set-ADGroup $Oldgroup -Description $Description}

	}

XML:

<Config> 
  <Settings>
	<CSV>.EmptyADgroup.csv</CSV>
	<Header>Group;sAMaccountname</Header>
	<Migrated>Deze Groep is gemigreerd naar de groep</Migrated>
  </Settings>
</Config>

CSV:

Oldgroup;Newgroup
Oldgroup;Newgroup

ZIP:

EmptyADgroup