Home > Back-end >  Create a powershell script to place a custom word template in the templates folder
Create a powershell script to place a custom word template in the templates folder

Time:09-17

I want to place a word template, template.dotm into the Word custom templates folder.

Using Office 365, latest version of Word. Windows 10. Apologies if my terminology is incorrect, still a powershell/programming novice.

This folder doesn't exist by default, and the directory Word looks for default templates in doesn't exist by default either. If a user has created a template, then it will create an expanding string named PersonalTemplates at the following registry key: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options, with the value being the directory they've elected as their default custom templates directory.

I want to make a script which:

  • Checks for presence of PersonalTemplates. If present, and value is not null, store as $regvalue.

  • If not present, or value is null, create expanding string with the following value $newreg at HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Word\Options.

  • Then copy template.dotm into the $regvalue or $newreg. Powershell will be run from the same directory as the template.dotm is stored in.

I've got a bunch of snippets which do some of the principle operations, though I can't work out how to tie them together, and am missing some bits which I just can't work out:

Copy the template to the destination

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"

Create registry key with value

Set-Location -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options'

New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' -Name 'PersonalTemplates' -Value "C:\Users$($user.Name)\Documents\Custom Office Templates" -PropertyType ExpandString -Force }

Get regvalue

$regvalue = (Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options' 'PersonalTemplates')

CodePudding user response:

I have put together your code snippets in order, also corrected the logic for checking if the Registry key is present or not.

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
}

I have not tested the creation of registry key as I am on my work laptop, so assuming that line of code works.

Also, question for you: With this approach wouldn't the registry entry have the single value that of the first user folder, you may have to look into the logic? I feel you may have to run this script for each user after they login using $env:Username instead of looping through the user folder. But I could be wrong, there may be other who could suggest better.

  • Related