I have a .xml file that I need to edit with PowerShell. I need to delete the nodes that are older than 6 months. "File Id=" is the node that I need to search for. The File Id is a date and I need to delete anything older than 182 days.
Example .xml below....
<?xml version="1.0" encoding="utf-8"?>
<Files>
<File Id="202304171534365167">
<ZipFile>N563UW_563_170420230254_C8D5900000B0_0A281140</ZipFile>
<Rds>CLT</Rds>
<FileName>N563UW_563_170420230254_C8D5900000B0_0A281140.fdr.sfd</FileName>
<Fleet>A32x1024</Fleet>
<FileDate>17 Apr 2023 02:54</FileDate>
<HoursofData>110.193</HoursofData>
<AircraftID>563</AircraftID>
<Status>1</Status>
</File>
<File Id="202101281622371208">
<ZipFile>N521UW_521_280120210948_C8D590000031_0A281114</ZipFile>
<Rds>CLT</Rds>
<FileName>N521UW_521_280120210948_C8D590000031_0A281114.fdr.sfd</FileName>
<Fleet>A32x_256</Fleet>
<FileDate>28 Jan 2021 09:48</FileDate>
<HoursofData>173.706</HoursofData>
<AircraftID>521</AircraftID>
<Status>1</Status>
</File>
<File Id="202101280823338850">
<ZipFile>N303RE_3RE_280120210224_C8D590000082_0A3C575B</ZipFile>
<Rds>TUL_H12_1</Rds>
<FileName>N303RE_3RE_280120210224_C8D590000082_0A3C575B.dlu.sfd</FileName>
<Fleet>737-8</Fleet>
<FileDate>28 Jan 2021 02:24</FileDate>
<HoursofData>26.8256</HoursofData>
<AircraftID>3RE</AircraftID>
<Status>1</Status>
</File>
</Files>
The code below will remove the File Id '202101281622371208' from the .xml file and saves it correctly, but I need it to find all the "File Id" nodes that are older than 182 days and delete them, so hard coding a specific date won't work. I can not wrap my head around how to do this. Any help greatly appreciated
# Need to delete entries older than 6 months
$XML_Date = Get-Date (Get-Date).AddDays(-182) -Format "yyyyMMddHHmmssms"
$xml = [xml](Get-Content D:\System\Index\Index.xml)
$node = $xml.SelectSingleNode("//File[@Id='202101281622371208']")
$node
$node.ParentNode.RemoveChild($node) | Out-Null
$xml.Save('D:\System\Index\Index.xml')