Hello everyone and welcome to “Continuous Improvement,” the podcast where we explore practical solutions for everyday challenges. I’m your host, Victor, and today we’ll be discussing a script that helps automate file renaming using PowerShell.

Yesterday, I encountered a scenario where I had to replace specific XML file names for a client’s Windows server that had no access to external networks or installations of third-party software. This task seemed daunting at first, but with a little creativity and the power of PowerShell, I found a solution.

Let’s take a closer look at the code involved. First, I needed to load all XML files from my designated folder. To accomplish this, I used the following line of code:

$files = Get-ChildItem C:\Users\victorleung\tw\Desktop\Test -Recurse -Include *.xml

This command allowed me to retrieve all XML files from the specified folder and its subfolders.

Next, I moved on to modifying the report names within the XML files. The code snippet below accomplishes this task:

$xmldata = [xml](Get-Content $file);
$name = $xmldata.ReportList.Report.GetAttribute("Name");
$name = $name + " (New)";
$xmldata.ReportList.Report.SetAttribute("Name", $name);
$xmldata.Save($file)

Here, we read the content of each file as XML data. We then access the specific attribute, “Name,” of the report element within the XML structure. By appending “ (New)” to the original name, we update the attribute value accordingly. Finally, we save the modified XML data back into the file.

Lastly, I wanted to change the file name from its original to a new naming convention. This can be achieved using the following code:

Get-ChildItem *.xml | Rename-Item -NewName { $_.Name -Replace '.xml$','-new.xml' }

This line of code uses the Rename-Item cmdlet to change the file names. We utilize a regular expression pattern to replace the “.xml” extension with “-new.xml.”

And voila! With these simple PowerShell lines, we were able to efficiently rename and modify hundreds of files without relying on external software installations or compromising security measures.

I hope you found this PowerShell script useful for your own file management tasks. Remember, continuous improvement is all about finding creative solutions to streamline our work processes.

If you have any questions or comments about today’s episode, feel free to reach out to me through our podcast’s website or social media channels. I’m always excited to hear from our listeners.

Thank you for tuning in to “Continuous Improvement.” Stay curious, stay inspired, and keep striving for improvement in all aspects of your life. Until next time!

[Theme music fades in and out]

[End of episode]