Rename and Edit xml files using powershell

Aakash Aggarwal
Aakash Aggarwal used Ask the Experts™
I have a folder and in that I have multiple sub-folders , each containing a default.xml file. I need to rename all the default.xml within all these sub-folders with the same name as that of the sub-folder.

So that my default.xml will become sub-folder_name.xml.

Lets say My main folder is \sites\ASDF, within that I have 10 sub-folders -> ABC-123, DEF-231, this and I want to rename the default.xml within all these sub-folder to ABC-123.xml, DEF-231.xml. It should be done by using Powershell. Any ideas?

rename-item –path "d:\a2\a11\SIT-E1236\default.xml" –newname d:\a2\a11\SIT-E1236\SIT-E1236.xml
rename-item –path "d:\a2\a11\SIT-E1238\default.xml" –newname d:\a2\a11\SIT-E1236\SIT-E1238.xml

This using some loop?

Also how can I edit some xml files together to make some particular changes on the xml nodes?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
pushd " C:\sites\ASDF"
dir -Directory | % {rename-item "C:\sites\ASDF\$($_.Name)\Default.xml" "$($_.Name).xml"}

Open in new window

Not checked here, test carefully
For question 2: do you want to modify data in the XML or change the structure?


Its like , I have One XML file in  d:/Site/Template/default.xml and parallel to the default.xml file and have two folders - CrsRep and my default.xml file contains the two nodes CrsRep and DOC. I need to automatically edit the nodes with the updated path of the folder.

I am using the below script to copy and rename all the folders.

$source = 'd:\Site\Template'
$target = 'd:\Site\'
$xmlPath = 'D:\Tenant.xml'

$xml = [xml](Get-Content -Path $xmlPath)
$envCode = $xml.SelectSingleNode('Tenants/ENVcode').InnerText.Trim()
$xml.SelectNodes('Tenants/Schools/School') | ForEach-Object {
      $name = "$($envCode)-$($_.InnerText.Trim())"
      $destination = Join-Path -Path $target -ChildPath $name
      Copy-Item -Path $source -Destination $destination -Recurse -Force -Verbose
      Get-ChildItem -Path $destination -Filter default.xml -Recurse | Rename-Item -NewName "$($name).xml"

After running this I will get all the folders with names listed in my Tenant file. Also inside all the folders, my default.xml file is renamed to name same as the folder name.xml. I just need to update the paths also inside all these new folder.xml with the corresponding new path in CrsRep and Doc Node.
Not sure if I get what you're trying to do
Have a look at Jeff Hicks excellent explanation of how to handle XML files in PowerShell:



Hi Michael,

I have folders and files in the below order.

Site -> SIT-1234 -> SIT-1234.xml, CrsRep, DocMan folders.
Site -> SIT-1245 -> SIT-1245.xml, CrsRep, DocMan folders.

 SIT-1234, SIT-2345 and such 100 more folders and inside all these there is one xml named as SIT-1234.xml, SIT-2345.xml and so on.
Below is my XML.

<?xml version="1.0" encoding="UTF-8"?>
    <name>Belgian Waffles</name>
   Two of our famous Belgian Waffles with plenty of real maple syrup
      <Crystal reports>a124.xml</Crystal reports>

I just need to update the DocMan and CrsRep node same as the path of these folders in all XMLs.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial