创建一个 powershell 脚本以将自定义 word 模板放入 templates 文件夹中
Create a powershell script to place a custom word template in the templates folder
我想将 Word 模板 template.dotm 放入 Word 自定义模板文件夹中。
使用 Office 365,最新版本的 Word。 Windows 10. 如果我的术语不正确,请见谅,我还是powershell/programming新手。
该文件夹默认不存在,Word查找默认模板的目录默认也不存在。如果用户创建了模板,则它将在以下注册表项中创建一个名为 PersonalTemplates 的扩展字符串:HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office.0\Word\Options,值为他们选择作为默认自定义模板的目录目录。
我想制作一个脚本:
检查是否存在 PersonalTemplates。如果存在且值不为空,则存储为 $regvalue。
如果不存在或值为空,则在 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office.0\Word\Options.
处创建具有以下值 $newreg 的扩展字符串
然后将template.dotm复制到$regvalue或$newreg中。 Powershell 将 运行 来自与存储 template.dotm 相同的目录。
我有一堆片段可以执行一些基本操作,但我不知道如何将它们联系在一起,并且遗漏了一些我无法解决的问题:
将模板复制到目的地
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) {
New-Item -ItemType Directory -Force -Path "C:\Users$($user.Name)\Documents\Custom Office Templates"
Copy-Item template.dotm -Destination "C:\Users$($user.Name)\Documents\Custom Office Templates"
使用值创建注册表项
Set-Location -Path
'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options'
New-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name
'PersonalTemplates' -Value "C:\Users$($user.Name)\Documents\Custom
Office Templates" -PropertyType ExpandString -Force }
获取正则值
$regvalue = (Get-ItemPropertyValue
'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options'
'PersonalTemplates')
我已经按顺序整理了您的代码片段,还更正了检查注册表项是否存在的逻辑。
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public))
{
$location = "C:\Users$($user.Name)\Documents\Custom Office Templates"
$IsPresent = Get-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' | ForEach-Object {If($_ -like '*PersonalTemplates*'){ Return 'True' }}
if(-Not($IsPresent -eq 'True'))
{
New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates' -Value $location -PropertyType ExpandString -Force \Not tested
New-Item -ItemType Directory -Force -Path $location
}
$existingValue= Get-ItemPropertyValue -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates'
if([string]::IsNullOrWhiteSpace($existingValue)){
Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates' -Value $location
}
else{
$location=$existingValue
if(!(test-path $existingValue))
{
New-Item -ItemType Directory -Force -Path $existingValue
}
}
Copy-Item template.dotm -Destination $location
}
我没有在我的工作笔记本电脑上测试注册表项的创建,因此假设该行代码有效。
另外,问你一个问题:如果使用这种方法,注册表项不会具有第一个用户文件夹的单一值,您可能需要查看逻辑?我觉得您可能必须 运行 每个用户在使用 $env:Username
登录后使用此脚本,而不是循环访问用户文件夹。但我可能是错的,也许还有其他人可以提出更好的建议。
我想将 Word 模板 template.dotm 放入 Word 自定义模板文件夹中。
使用 Office 365,最新版本的 Word。 Windows 10. 如果我的术语不正确,请见谅,我还是powershell/programming新手。
该文件夹默认不存在,Word查找默认模板的目录默认也不存在。如果用户创建了模板,则它将在以下注册表项中创建一个名为 PersonalTemplates 的扩展字符串:HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office.0\Word\Options,值为他们选择作为默认自定义模板的目录目录。
我想制作一个脚本:
检查是否存在 PersonalTemplates。如果存在且值不为空,则存储为 $regvalue。
如果不存在或值为空,则在 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office.0\Word\Options.
处创建具有以下值 $newreg 的扩展字符串然后将template.dotm复制到$regvalue或$newreg中。 Powershell 将 运行 来自与存储 template.dotm 相同的目录。
我有一堆片段可以执行一些基本操作,但我不知道如何将它们联系在一起,并且遗漏了一些我无法解决的问题:
将模板复制到目的地
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public)) { New-Item -ItemType Directory -Force -Path "C:\Users$($user.Name)\Documents\Custom Office Templates" Copy-Item template.dotm -Destination "C:\Users$($user.Name)\Documents\Custom Office Templates"
使用值创建注册表项
Set-Location -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options'
New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates' -Value "C:\Users$($user.Name)\Documents\Custom Office Templates" -PropertyType ExpandString -Force }
获取正则值
$regvalue = (Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' 'PersonalTemplates')
我已经按顺序整理了您的代码片段,还更正了检查注册表项是否存在的逻辑。
ForEach ($user in (Get-ChildItem "C:\Users" -Exclude Public))
{
$location = "C:\Users$($user.Name)\Documents\Custom Office Templates"
$IsPresent = Get-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' | ForEach-Object {If($_ -like '*PersonalTemplates*'){ Return 'True' }}
if(-Not($IsPresent -eq 'True'))
{
New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates' -Value $location -PropertyType ExpandString -Force \Not tested
New-Item -ItemType Directory -Force -Path $location
}
$existingValue= Get-ItemPropertyValue -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates'
if([string]::IsNullOrWhiteSpace($existingValue)){
Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office.0\Word\Options' -Name 'PersonalTemplates' -Value $location
}
else{
$location=$existingValue
if(!(test-path $existingValue))
{
New-Item -ItemType Directory -Force -Path $existingValue
}
}
Copy-Item template.dotm -Destination $location
}
我没有在我的工作笔记本电脑上测试注册表项的创建,因此假设该行代码有效。
另外,问你一个问题:如果使用这种方法,注册表项不会具有第一个用户文件夹的单一值,您可能需要查看逻辑?我觉得您可能必须 运行 每个用户在使用 $env:Username
登录后使用此脚本,而不是循环访问用户文件夹。但我可能是错的,也许还有其他人可以提出更好的建议。