0

Considering..

Orig.xml

<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="COM">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Open All COM Sessions.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Close all COM Sessions.lnk" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>

StartMenuLayout-MOD.xml

<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="My-Tools">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="Microsoft.AutoGenerated.{20529041-3E03-352B-AC38-58B1C8A14CBF}" />
        </start:Group>
        <start:Group Name="COM">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Open All COM Sessions.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Close all COM Sessions.lnk" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>

I would like to programmatically add the My-Tools section (shown in StartMenuLayout-MOD.xml) to Orig.xml so that both files end up identical.

Below, is what I have so far; which is probably wrong:

[xml]$xmlOrig = Get-Content C:\Orig.xml
[xml]$xmlDoc = Get-Content C:\StartMenuLayout-MOD.xml

$child = $xmlDoc.LayoutModificationTemplate.DefaultLayoutOverride.StartLayoutCollection.StartLayout.group | ?{$_.Name -eq "My-Tools"}

Ultimately, I would like to save the contents of $child to my script.. so, I can add it to an existing XML file that's structured similarly without needing StartMenuLayout-MOD.xml.

0

1 Answer 1

2

Working off your existing code, you have to import the node and then prepend it.

[xml]$xmlOrig = Get-Content C:\Orig.xml
[xml]$xmlDoc = Get-Content C:\StartMenuLayout-MOD.xml

$child = $xmlDoc.LayoutModificationTemplate.DefaultLayoutOverride.StartLayoutCollection.StartLayout.group | ?{$_.Name -eq "My-Tools"}

$newnode = $xmlOrig.ImportNode($child,$true)

$xmlOrig.LayoutModificationTemplate.DefaultLayoutOverride.StartLayoutCollection.StartLayout.prependchild($newnode)

$xmlOrig.save('C:\Orig.xml')

You could just add this in your script so you don't have to pull from a file

[xml]$xmlDoc = @'
<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <LayoutOptions StartTileGroupCellWidth="6" />
  <DefaultLayoutOverride>
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="My-Tools">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="Microsoft.AutoGenerated.{20529041-3E03-352B-AC38-58B1C8A14CBF}" />
        </start:Group>
        <start:Group Name="COM">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Open All COM Sessions.lnk" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Close all COM Sessions.lnk" />
        </start:Group>
        <start:Group Name="Office">
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>
'@

This is equivalent to the Get-Content line.

Sign up to request clarification or add additional context in comments.

5 Comments

I'd run it again. $child | get-member should show typename: System.Xml.XmlElement#http://schemas.microsoft.com/Start/2014/StartLayout#Group
Thank you so much. How can I artificially create $newnode in a PowerShell script (so I don't have to mess with $xmlDoc at all)? How can I export $newnode's contents into my Powershell script?
No worries if its too much work to add this enhancement to you answer. I'll give you the credit for it anyway. I really appreciate your time.
I’m not sure but what if you just put that chunk of text in your script? Give me a few and I’ll add example
I got it all working! Both as a chunk of text in the script as well as gc from a file. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.