英文:
Editing Taskbar Layout modification with Powershell
问题
我正在为我的公司准备Windows 11的部署,并找到了一个可以用于设置自定义图标固定到任务栏的XML文件。
我想在安装过程中编辑XML文档,比如说,如果计算机已安装了Office,我想将Word和Outlook图标固定到任务栏。
以下是XML内容:
<?xml version="1.0" encoding="utf-8"?>
<LayoutModificationTemplate
xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
Version="1">
<CustomTaskbarLayoutCollection PinListPlacement="Replace">
<defaultlayout:TaskbarLayout>
<taskbar:TaskbarPinList>
<taskbar:DesktopApp DesktopApplicationID="Microsoft.Windows.Explorer"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" />
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
</taskbar:TaskbarPinList>
</defaultlayout:TaskbarLayout>
</CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>
我应该如何在前4个图标之后添加以下行:
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />
我已经找到了如何在PowerShell中打开XML文件,但我不知道如何在XML的正确部分添加行。
谢谢你的帮助!
英文:
I am preparing Windows 11 deployment for my company and I found an XML that I can use for setting up customised icons pinned to taskbar .
I want to edit XML document during install process - let's say - if computer has Office installed I want Word and Outlook icons pinned to taskbar.
This is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LayoutModificationTemplate
xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
Version="1">
<CustomTaskbarLayoutCollection PinListPlacement="Replace">
<defaultlayout:TaskbarLayout>
<taskbar:TaskbarPinList>
<taskbar:DesktopApp DesktopApplicationID="Microsoft.Windows.Explorer"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" />
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
</taskbar:TaskbarPinList>
</defaultlayout:TaskbarLayout>
</CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>
How can I add a line
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />
to this XML after first 4 icons?
I found out how to open XML in Powershell, but I am lost how to add line in the correct part of XML.
Thank you for your help!
答案1
得分: 1
以下代码可以实现你的要求,你只需要根据你的IF语句或其他条件来决定要添加的内容。目前它只添加了你提供的Outlook示例,但你可以定义任意数量的NewChilds,并在foreach中导入它们:
# 定义XML内容
[xml]$xmlDocument = '<?xml version="1.0" encoding="utf-8"?>
<LayoutModificationTemplate
xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
Version="1">
<CustomTaskbarLayoutCollection PinListPlacement="Replace">
<defaultlayout:TaskbarLayout>
<taskbar:TaskbarPinList>
<taskbar:DesktopApp DesktopApplicationID="Microsoft.Windows.Explorer"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" />
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
</taskbar:TaskbarPinList>
</defaultlayout:TaskbarLayout>
</CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>'
# 定义要添加的子节点
[xml]$NewChild = @"
<Grid xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout">
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />
</Grid>
"@
# 创建命名空间管理器,并在TaskbarPinList的最后一个子节点后插入新的子节点
# 由于非默认命名空间"taskbar",所以需要命名空间
$nsManager = New-Object System.Xml.XmlNamespaceManager($xmlDocument.NameTable)
$nsManager.AddNamespace("taskbar", "http://schemas.microsoft.com/Start/2014/TaskbarLayout")
# 将新的子节点导入XML文档以创建节点
$targetNode = $xmlDocument.SelectSingleNode("//taskbar:TaskbarPinList", $nsManager)
# 在目标位置插入节点
$newNode = $xmlDocument.ImportNode($NewChild.Grid.SelectSingleNode('taskbar:DesktopApp', $nsManager),$true)
[VOID]$targetNode.InsertAfter($newNode, $targetNode.LastChild)
# 将XML保存到所需的位置
$xmlDocument.Save('文件路径或包含路径的变量')
请将"文件路径或包含路径的变量"替换为你想要保存XML的路径。
英文:
The below should do the trick, you will just need to add if your IF statements or whatever to decide what you want to add. Currently it only adds the one example you provided for Outlook, but you can define as many NewChilds as you want, and import them in a foreach:
# Define the XML content
[xml]$xmlDocument = '<?xml version="1.0" encoding="utf-8"?>
<LayoutModificationTemplate
xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
Version="1">
<CustomTaskbarLayoutCollection PinListPlacement="Replace">
<defaultlayout:TaskbarLayout>
<taskbar:TaskbarPinList>
<taskbar:DesktopApp DesktopApplicationID="Microsoft.Windows.Explorer"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"/>
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" />
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
</taskbar:TaskbarPinList>
</defaultlayout:TaskbarLayout>
</CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>'
# Define the child to be added
[xml]$NewChild = @"
<Grid xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout">
<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />
</Grid>
"@
# Create a namespace manager and Insert the new child after the last one under TaskbarPinList
# Namespace is required due to none default namespace "taskbar"
$nsManager = New-Object System.Xml.XmlNamespaceManager($xmlDocument.NameTable)
$nsManager.AddNamespace("taskbar", "http://schemas.microsoft.com/Start/2014/TaskbarLayout")
#Import the new child into the XML doc to create a node
$targetNode = $xmlDocument.SelectSingleNode("//taskbar:TaskbarPinList", $nsManager)
#Insert the node where we want it
$newNode = $xmlDocument.ImportNode($NewChild.Grid.SelectSingleNode('taskbar:DesktopApp', $nsManager),$true)
[VOID]$targetNode.InsertAfter($newNode, $targetNode.LastChild)
#Save the XML where ever you require
$xmlDocument.Save('FILE PATH OR VARIABLE CONTAINING A PATH HERE')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论