【发布时间】:2021-11-05 17:54:18
【问题描述】:
我想将 word 模板 template.dotm 放入 Word 自定义模板文件夹中。
使用 Office 365,最新版本的 Word。 Windows 10。抱歉,如果我的术语不正确,仍然是 powershell/编程新手。
该文件夹默认不存在,Word在其中查找默认模板的目录默认也不存在。如果用户创建了一个模板,那么它将在以下注册表项中创建一个名为 PersonalTemplates 的扩展字符串:HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options,其值是他们选择的默认目录自定义模板目录。
我想制作一个脚本:
-
检查 PersonalTemplates 的存在。如果存在,并且 value 不为 null,则存储为 $regvalue。
-
如果不存在,或者值为 null,则在 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options 处创建具有以下值 $newreg 的扩展字符串。
-
然后将 template.dotm 复制到 $regvalue 或 $newreg 中。 Powershell 将从存储 template.dotm 的同一目录运行。
我有一堆 sn-ps 可以执行一些主要操作,但我不知道如何将它们联系在一起,并且缺少一些我无法解决的部分:
将模板复制到目的地
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) { 新项目 -ItemType 目录 -Force -Path "C:\Users$($user.Name)\Documents\Custom Office Templates" Copy-Item template.dotm -Destination "C:\Users$($user.Name)\Documents\Custom Office Templates"
使用值创建注册表项
设置-位置-路径 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options'
New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -名称 'PersonalTemplates' -Value "C:\Users$($user.Name)\Documents\Custom Office 模板”-PropertyType ExpandString -Force }
获取正则值
$regvalue = (Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' '个人模板')
【问题讨论】:
标签: powershell templates registry word