SOLVED

new content after last match of a Pattern in XML file

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3136180%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3Enew%20content%20after%20last%20match%20of%20a%20Pattern%20in%20XML%20file%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3136180%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EHi%20I%20am%20trying%20to%20use%20Powershell%20to%20add%20content%20to%20an%20XML%20file%20after%20the%20last%20match%20of%20a%20pattern%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EI%20need%20to%20add%20the%20content%20from%20%24OutTemp%20txt%20file%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%22%3C!--%20steve.cox%20-%20test3%20-02%2F02%2F2022%2011%3A01%3A36%20--%5C--%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5C%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_2.0%26amp%3Bamp%3BREV_PMAP%5C%5C5B8207000061%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3C!--%20steve.cox%20-%20test4%20-%20-02%2F02%2F2022%2011%3A01%3A36%20--%5C--%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5C%5CDISK%26amp%3Bamp%3BVEN_TOSHIBA%26amp%3Bamp%3BPROD_SILICON-POWER%26amp%3Bamp%3BREV_PMAP%5C%5CMSFTNAKTM1070837BA061C1622%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%22%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ETo%20below%20the%20last%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%20line%20of%20the%20USB-Groups.XML%20File%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3CP%3E%3CGROUPS%3E%3CBR%20%2F%3E%3CGROUP%20id%3D%22%26quot%3B%7B9b28fae8-72f7-4267-a1a5-685f747a7146%7D%26quot%3B%22%3E%3CBR%20%2F%3E%3C!--%20.%2FVendor%2FMSFT%2FDefender%2FConfiguration%2FDeviceControl%2FPolicyGroups%2F%257b9b28fae8-72f7-4267-a1a5-685f747a7146%257d%2FGroupData%20--%5C--%3E%3CBR%20%2F%3E%3CMATCHTYPE%3EMatchAny%3C%2FMATCHTYPE%3E%3CBR%20%2F%3E%3CDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3CPRIMARYID%3ERemovableMediaDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3CPRIMARYID%3ECdRomDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3CPRIMARYID%3EWpdDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3C%2FDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3C%2FGROUP%3E%3CBR%20%2F%3E%3CGROUP%20id%3D%22%26quot%3B%7B65fa649a-a111-4912-9294-fb6337a25038%7D%26quot%3B%22%3E%3CBR%20%2F%3E%3CMATCHTYPE%3EMatchAny%3C%2FMATCHTYPE%3E%3CBR%20%2F%3E%3CDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5C%5CDISK%26amp%3Bamp%3BVEN_TOSHIBA%26amp%3Bamp%3BPROD_SILICON-POWER%26amp%3Bamp%3BREV_PMAP%5C%5CMSFTNAKTM10708466023196668%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5C%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_2.0%26amp%3Bamp%3BREV_0000%5C%5C408D5C1EF726102179650693%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5C%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_3.0%26amp%3Bamp%3BREV_PMAP%5C%5C60A44C42651BB2A04626FC36%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5C%5CDISK%26amp%3Bamp%3BVEN_SANDISK%26amp%3Bamp%3BPROD_CRUZER_EDGE%26amp%3Bamp%3BREV_1.26%5C%5C20043514001B91029E82%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3C%2FDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3C%2FGROUP%3E%3CBR%20%2F%3E%3C%2FGROUPS%3E%22%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EIf%20I%20use%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%22%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EFunction%20WritetoXML%20%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%24NewLines%20%3D%20Get-Content%20-Path%20%24OutTemp%20%7C%20%3F%7B%24_.Trim()%20-ne%20%22%22%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%24New%20%3D%20Get-Content%20-Path%20%24XMLPath%20%7C%3CBR%20%2F%3EForEach-Object%20%7B%3CBR%20%2F%3E%24_%3CBR%20%2F%3EIf(%24_-match%20('%3C%2FP%3E%3C%2FLINGO-SUB%3E'))%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24NewLines%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%26lt%3B%5C%2FP%26gt%3B%3CP%3E%24New%20%7C%20Out-File%20-FilePath%20%24XMLPath%20-Force%3CBR%20%2F%3ERemove-Item%20%24OutTemp%3CBR%20%2F%3E%7D%22%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eit%20writes%20after%20each%20instance%20of%26nbsp%3B''%20but%20cannot%20work%20out%20how%20to%20get%20this%20to%20write%20to%20only%20after%20the%20last%20instance%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3136180%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EWindows%20Defender%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FP%3E%20line%20of%20the%20USB-Groups.XML%20File%26nbsp%3B%3CP%3E%3C%2FP%3E%3CP%3E%3CGROUPS%3E%3CBR%20%2F%3E%3CGROUP%20id%3D%22%26quot%3B%7B9b28fae8-72f7-4267-a1a5-685f747a7146%7D%26quot%3B%22%3E%3CBR%20%2F%3E%3C!--%20.%2FVendor%2FMSFT%2FDefender%2FConfiguration%2FDeviceControl%2FPolicyGroups%2F%257b9b28fae8-72f7-4267-a1a5-685f747a7146%257d%2FGroupData%20--%3E%3CBR%20%2F%3E%3CMATCHTYPE%3EMatchAny%3C%2FMATCHTYPE%3E%3CBR%20%2F%3E%3CDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3CPRIMARYID%3ERemovableMediaDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3CPRIMARYID%3ECdRomDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3CPRIMARYID%3EWpdDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3C%2FDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3C%2FGROUP%3E%3CBR%20%2F%3E%3CGROUP%20id%3D%22%26quot%3B%7B65fa649a-a111-4912-9294-fb6337a25038%7D%26quot%3B%22%3E%3CBR%20%2F%3E%3CMATCHTYPE%3EMatchAny%3C%2FMATCHTYPE%3E%3CBR%20%2F%3E%3CDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_TOSHIBA%26amp%3Bamp%3BPROD_SILICON-POWER%26amp%3Bamp%3BREV_PMAP%5CMSFTNAKTM10708466023196668%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_2.0%26amp%3Bamp%3BREV_0000%5C408D5C1EF726102179650693%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_3.0%26amp%3Bamp%3BREV_PMAP%5C60A44C42651BB2A04626FC36%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_SANDISK%26amp%3Bamp%3BPROD_CRUZER_EDGE%26amp%3Bamp%3BREV_1.26%5C20043514001B91029E82%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3C%2FDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3C%2FGROUP%3E%3CBR%20%2F%3E%3C%2FGROUPS%3E%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20use%26nbsp%3B%3C%2FP%3E%3CP%3E%22%3C%2FP%3E%3CP%3EFunction%20WritetoXML%20%7B%3C%2FP%3E%3CP%3E%24NewLines%20%3D%20Get-Content%20-Path%20%24OutTemp%20%7C%20%3F%7B%24_.Trim()%20-ne%20%22%22%7D%3C%2FP%3E%3CP%3E%24New%20%3D%20Get-Content%20-Path%20%24XMLPath%20%7C%3CBR%20%2F%3EForEach-Object%20%7B%3CBR%20%2F%3E%24_%3CBR%20%2F%3EIf(%24_-match%20(''))%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24NewLines%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%24New%20%7C%20Out-File%20-FilePath%20%24XMLPath%20-Force%3CBR%20%2F%3ERemove-Item%20%24OutTemp%3CBR%20%2F%3E%7D%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eit%20writes%20after%20each%20instance%20of%26nbsp%3B''%20but%20cannot%20work%20out%20how%20to%20get%20this%20to%20write%20to%20only%20after%20the%20last%20instance%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3136180%22%20slang%3D%22en-US%22%3Enew%20content%20after%20last%20match%20of%20a%20Pattern%20in%20XML%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3136180%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20I%20am%20trying%20to%20use%20Powershell%20to%20add%20content%20to%20an%20XML%20file%20after%20the%20last%20match%20of%20a%20pattern%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20add%20the%20content%20from%20%24OutTemp%20txt%20file%26nbsp%3B%3C%2FP%3E%3CP%3E%22%3C!--%20steve.cox%20-%20test3%20-02%2F02%2F2022%2011%3A01%3A36%20--%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_2.0%26amp%3Bamp%3BREV_PMAP%5C5B8207000061%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3C!--%20steve.cox%20-%20test4%20-%20-02%2F02%2F2022%2011%3A01%3A36%20--%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_TOSHIBA%26amp%3Bamp%3BPROD_SILICON-POWER%26amp%3Bamp%3BREV_PMAP%5CMSFTNAKTM1070837BA061C1622%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20below%20the%20last%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%20line%20of%20the%20USB-Groups.XML%20File%26nbsp%3B%3CP%3E%3C%2FP%3E%3CP%3E%3CGROUPS%3E%3CBR%20%2F%3E%3CGROUP%20id%3D%22%26quot%3B%7B9b28fae8-72f7-4267-a1a5-685f747a7146%7D%26quot%3B%22%3E%3CBR%20%2F%3E%3C!--%20.%2FVendor%2FMSFT%2FDefender%2FConfiguration%2FDeviceControl%2FPolicyGroups%2F%257b9b28fae8-72f7-4267-a1a5-685f747a7146%257d%2FGroupData%20--%3E%3CBR%20%2F%3E%3CMATCHTYPE%3EMatchAny%3C%2FMATCHTYPE%3E%3CBR%20%2F%3E%3CDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3CPRIMARYID%3ERemovableMediaDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3CPRIMARYID%3ECdRomDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3CPRIMARYID%3EWpdDevices%3C%2FPRIMARYID%3E%3CBR%20%2F%3E%3C%2FDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3C%2FGROUP%3E%3CBR%20%2F%3E%3CGROUP%20id%3D%22%26quot%3B%7B65fa649a-a111-4912-9294-fb6337a25038%7D%26quot%3B%22%3E%3CBR%20%2F%3E%3CMATCHTYPE%3EMatchAny%3C%2FMATCHTYPE%3E%3CBR%20%2F%3E%3CDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_TOSHIBA%26amp%3Bamp%3BPROD_SILICON-POWER%26amp%3Bamp%3BREV_PMAP%5CMSFTNAKTM10708466023196668%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_2.0%26amp%3Bamp%3BREV_0000%5C408D5C1EF726102179650693%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_KINGSTON%26amp%3Bamp%3BPROD_DATATRAVELER_3.0%26amp%3Bamp%3BREV_PMAP%5C60A44C42651BB2A04626FC36%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3CINSTANCEPATHID%3EUSBSTOR%5CDISK%26amp%3Bamp%3BVEN_SANDISK%26amp%3Bamp%3BPROD_CRUZER_EDGE%26amp%3Bamp%3BREV_1.26%5C20043514001B91029E82%26amp%3Bamp%3B0%3C%2FINSTANCEPATHID%3E%3CBR%20%2F%3E%3C%2FDESCRIPTORIDLIST%3E%3CBR%20%2F%3E%3C%2FGROUP%3E%3CBR%20%2F%3E%3C%2FGROUPS%3E%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20use%26nbsp%3B%3C%2FP%3E%3CP%3E%22%3C%2FP%3E%3CP%3EFunction%20WritetoXML%20%7B%3C%2FP%3E%3CP%3E%24NewLines%20%3D%20Get-Content%20-Path%20%24OutTemp%20%7C%20%3F%7B%24_.Trim()%20-ne%20%22%22%7D%3C%2FP%3E%3CP%3E%24New%20%3D%20Get-Content%20-Path%20%24XMLPath%20%7C%3CBR%20%2F%3EForEach-Object%20%7B%3CBR%20%2F%3E%24_%3CBR%20%2F%3EIf(%24_-match%20(''))%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24NewLines%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%24New%20%7C%20Out-File%20-FilePath%20%24XMLPath%20-Force%3CBR%20%2F%3ERemove-Item%20%24OutTemp%3CBR%20%2F%3E%7D%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eit%20writes%20after%20each%20instance%20of%26nbsp%3B''%20but%20cannot%20work%20out%20how%20to%20get%20this%20to%20write%20to%20only%20after%20the%20last%20instance%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3136180%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20Defender%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hi I am trying to use Powershell to add content to an XML file after the last match of a pattern 

I need to add the content from $OutTemp txt file 

"<!-- steve.cox - test3 -02/02/2022 11:01:36 -->
<InstancePathId>USBSTOR\DISK&amp;VEN_KINGSTON&amp;PROD_DATATRAVELER_2.0&amp;REV_PMAP\5B8207000061&amp;0</InstancePathId>
<!-- steve.cox - test4 - -02/02/2022 11:01:36 -->
<InstancePathId>USBSTOR\DISK&amp;VEN_TOSHIBA&amp;PROD_SILICON-POWER&amp;REV_PMAP\MSFTNAKTM1070837BA061C1622&amp;0</InstancePathId>"

 

To below the last </InstancePathId> line of the USB-Groups.XML File 

<Groups>
<Group Id="{9b28fae8-72f7-4267-a1a5-685f747a7146}">
<!-- ./Vendor/MSFT/Defender/Configuration/DeviceControl/PolicyGroups/%7b9b28fae8-72f7-4267-a1a5-685f747a7146%7d/GroupData -->
<MatchType>MatchAny</MatchType>
<DescriptorIdList>
<PrimaryId>RemovableMediaDevices</PrimaryId>
<PrimaryId>CdRomDevices</PrimaryId>
<PrimaryId>WpdDevices</PrimaryId>
</DescriptorIdList>
</Group>
<Group Id="{65fa649a-a111-4912-9294-fb6337a25038}">
<MatchType>MatchAny</MatchType>
<DescriptorIdList>
<InstancePathId>USBSTOR\DISK&amp;VEN_TOSHIBA&amp;PROD_SILICON-POWER&amp;REV_PMAP\MSFTNAKTM10708466023196668&amp;0</InstancePathId>
<InstancePathId>USBSTOR\DISK&amp;VEN_KINGSTON&amp;PROD_DATATRAVELER_2.0&amp;REV_0000\408D5C1EF726102179650693&amp;0</InstancePathId>
<InstancePathId>USBSTOR\DISK&amp;VEN_KINGSTON&amp;PROD_DATATRAVELER_3.0&amp;REV_PMAP\60A44C42651BB2A04626FC36&amp;0</InstancePathId>
<InstancePathId>USBSTOR\DISK&amp;VEN_SANDISK&amp;PROD_CRUZER_EDGE&amp;REV_1.26\20043514001B91029E82&amp;0</InstancePathId>
</DescriptorIdList>
</Group>
</Groups>"

 

If I use 

"

Function WritetoXML {

$NewLines = Get-Content -Path $OutTemp | ?{$_.Trim() -ne ""}

$New = Get-Content -Path $XMLPath |
ForEach-Object {
$_
If($_-match ('</InstancePathId>'))
{
$NewLines
}
}

$New | Out-File -FilePath $XMLPath -Force
Remove-Item $OutTemp
}"

 

it writes after each instance of '</InstancePathId>' but cannot work out how to get this to write to only after the last instance 

 

 

4 Replies

@SteveCox 

Hi

I think this should help

 

$NewLines = Get-Content -Path C:\Users\f.malaeb\OutTemp.txt | ?{$_.Trim() -ne ""}
[System.Collections.ArrayList]$New = Get-Content -Path C:\Users\f.malaeb\XML.xml 
$IndexNumber=(($new | Select-String "<InstancePathId>").LineNumber)[-1]
$new.Insert($IndexNumber,$NewLines)

$new

The Expected output is 

<Groups>
<Group Id="{9b28fae8-72f7-4267-a1a5-685f747a7146}">
<!-- ./Vendor/MSFT/Defender/Configuration/DeviceControl/PolicyGroups/%7b9b28fae8-72f7-4267-a1a5-685f747a7146%7d/GroupData -->
<MatchType>MatchAny</MatchType>
<DescriptorIdList>
<PrimaryId>RemovableMediaDevices</PrimaryId>
<PrimaryId>CdRomDevices</PrimaryId>
<PrimaryId>WpdDevices</PrimaryId>
</DescriptorIdList>
</Group>
<Group Id="{65fa649a-a111-4912-9294-fb6337a25038}">
<MatchType>MatchAny</MatchType>
<DescriptorIdList>
<InstancePathId>USBSTOR\DISK&amp;VEN_TOSHIBA&amp;PROD_SILICON-POWER&amp;REV_PMAP\MSFTNAKTM10708466023196668&amp;0</InstancePathId>
<InstancePathId>USBSTOR\DISK&amp;VEN_KINGSTON&amp;PROD_DATATRAVELER_2.0&amp;REV_0000\408D5C1EF726102179650693&amp;0</InstancePathId>
<InstancePathId>USBSTOR\DISK&amp;VEN_KINGSTON&amp;PROD_DATATRAVELER_3.0&amp;REV_PMAP\60A44C42651BB2A04626FC36&amp;0</InstancePathId>
<InstancePathId>USBSTOR\DISK&amp;VEN_SANDISK&amp;PROD_CRUZER_EDGE&amp;REV_1.26\20043514001B91029E82&amp;0</InstancePathId>
<!-- steve.cox - test3 -02/02/2022 11:01:36 -->
<InstancePathId>USBSTOR\DISK&amp;VEN_KINGSTON&amp;PROD_DATATRAVELER_2.0&amp;REV_PMAP\5B8207000061&amp;0</InstancePathId>
<!-- steve.cox - test4 - -02/02/2022 11:01:36 -->
<InstancePathId>USBSTOR\DISK&amp;VEN_TOSHIBA&amp;PROD_SILICON-POWER&amp;REV_PMAP\MSFTNAKTM1070837BA061C1622&amp;0</InstancePathId>
</DescriptorIdList>
</Group>
</Groups>

 

Did this one work?
Hi yes thanks for the help worked perfectly, still had an issue with the Encoding of the XML output being Created as UTF-16 LE BOM so wouldn't Parse correctly so fixed this with this line to save a UTF-8 "[IO.File]::WriteAllLines($XMLPath, $BOMFile)" and also found that Defender doesn't Like Comments in the "Descriptor ID" so have removed them and created change log in a different Location So if anyone is trying to Use Scripts to Edit the XML Files for Defender USB Blocking watch out for Encoding and Comments Thanks again for the assistance
best response confirmed by SteveCox (Occasional Contributor)
Solution
Glad it help.
Kindly mark the answer as best response.
For the XML, I need to check it, and also you can start a new post as it also be better in the search engines.

Thanks