【问题标题】:Create a powershell script to place a custom word template in the templates folder创建 powershell 脚本以将自定义单词模板放置在模板文件夹中
【发布时间】: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


    【解决方案1】:

    我已按顺序整理了您的代码 sn-ps,还更正了检查注册表项是否存在的逻辑。

    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\16.0\Word\Options' | ForEach-Object {If($_ -like '*PersonalTemplates*'){ Return 'True' }}
      if(-Not($IsPresent -eq 'True'))
      { 
        New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.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\16.0\Word\Options' -Name 'PersonalTemplates'
    
      if([string]::IsNullOrWhiteSpace($existingValue)){
       Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.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 登录后为他们运行此脚本,而不是遍历用户文件夹。但我可能是错的,可能还有其他人可以提出更好的建议。

    【讨论】:

    • 感谢您的快速跟进,非常感谢!我很快就会试试这个。事实上,你提出的问题可能是个问题。在这种情况下,我可以处理它,因为它将在其上运行的机器组具有一组已知的用户帐户,管理员、用户、公共,因此我可以排除管理员和公共帐户,然后它们将只包括用户帐户。但也许更好的方法是以某种方式选择当前登录的用户?
    • 是的,这就是我的假设。您可以使用变量 $env:Username 获取当前登录的用户名,该变量与 users 文件夹下的文件夹名称相同。
    • 嗨,刚刚有机会看看这个。谢谢,这很有帮助,但是它不满足使用注册表值作为目录以将文件复制到的要求之一,如果注册表存在并且具有值。有任何想法吗?提前致谢!
    • 我已经更新了使用注册表中的目录的逻辑,如果它已经存在的话。抱歉,我没有测试代码,因为我现在没有机器,但逻辑差不多。
    • 您好,谢谢!这又有帮助了。我(想我已经)简化了这个并将其发布为另一个答案(我已经测试了我的答案,似乎工作正常)。我想我们快到了……但是我还需要应对 $IsPresent 为真但 $regvalue 为空或为空的情况。我认为这样做的好方法是 ($regvalue -notlike '*:*')。任何适合您或我的答案的想法?再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 2012-12-20
    • 2021-07-02
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多