Increment PowerGUI Xml Version with Powershell

If you are using PowerGUI (which you should) and some of your collaegues do too, you might want to use a central configuration. Whenever you want to update the central configuration xml file, you need to increment the version number in order to push this change out to your collaegues. The following function increments the version number for you and even allows you to store the new file to the central location at the same time. All without having to edit the complex xml file manually. It even saves a backup copy of your central config file in case you mess up ;) It assumes you use a simple x.y version number, so please start out with 2.0 when setting up your config.
To update your central config: just make the modifications within PowerGUI and then run this function.

Function Increment-PowerGuiXmlVersion
	{
	Param(
	$PersonalXmlFile = (Join-Path $env:appdata "Quest Software\PowerGUI\config.xml"),
	$CentralXmlFile = 'P:\Centrally Managed Files\config.xml',
	$XPath = "//container[@name='version']",
	$IncrementMajor = $false,
	$IncrementMinor = $true
	)
 
	# Load central xml file (for backup and reading the current version number)
	$CentralConfig = New-Object System.Xml.XmlDocument
	$CentralConfig.Load($CentralXmlFile)
 
	# Select the correct node
	$NodeToRead = $CentralConfig.SelectSingleNode($XPath)
 
	# Read the current version
	$CurrentVersion = $NodeToRead.Value
	$MajorVersion = [Int]($CurrentVersion.Split(".")[0])
	$MinorVersion = [Int]($CurrentVersion.Split(".")[1])
	Write-Host "Current Version of Central Xml File: $CurrentVersion"
 
	# Backup central xml file
	$BackupFileName = $CentralXmlFile.Replace(".xml", ("_{0}.xml" -f $CurrentVersion))
	$CentralConfig.Save($BackupFileName)
 
	# Load personal xml file
	$PersonalConfig = New-Object System.Xml.XmlDocument
	$PersonalConfig.Load($PersonalXmlFile)
 
	# Select the correct node
	$NodeToBeChanged = $PersonalConfig.SelectSingleNode($XPath)
 
	# Increment the version
	If ($IncrementMinor){$MinorVersion++}
	If ($IncrementMajor){$MajorVersion++; $MinorVersion = 0}
	$newVersion = [string]::Concat($MajorVersion,".",$MinorVersion)
	Write-Host "New Version of Central Xml File: $newVersion"
 
	# Write the new version
	$NodeToBeChanged.Value = $newVersion
 
	# Save the personal xml file as the new central xml file
	$PersonalConfig.Save($CentralXmlFile)	
	}

»crosslinked«

One thought on “Increment PowerGUI Xml Version with Powershell”

Leave a Reply