我想把一個word模板template.dotm放到Word自定義模板檔案夾中。
使用Office 365,最新版本的Word。Windows 10。如果我的術語不正確,請原諒,我還是一個Powershell/編程新手。
這個檔案夾默認不存在,而Word尋找默認模板的目錄也默認不存在。如果用戶創建了一個模板,那么它將在以下注冊表鍵中創建一個名為 PersonalTemplates 的擴展字串:HKEY_CURRENT_USERSOFTWAREMicrosoftOffice16.0WordOptions,其值是他們選擇的默認自定義模板目錄。
我想做一個腳本:
我想做一個腳本。
檢查是否存在 PersonalTemplates。如果存在,并且值不是空的,則存盤為$regvalue。
如果不存在,或者值為空,在HKEY_CURRENT_USERSOFTWAREMicrosoftOffice16.0WordOptions.
創建擴展字串,值為$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)DocumentsCustom Office Templates" Copy-Item template.dotm -Destination "C:Users$($user.Name)DocumentsCustom Office Templates"
。創建帶有值的注冊表鍵
Set-Location -Path 'HKCU:SOFTWAREMicrosoftOffice16.0WordOptions'
New-ItemProperty -Path 'HKCU:SOFTWAREMicrosoftOffice16.0WordOptions' -Name PersonalTemplates' -Value "C:Users$($user.Name)DocumentsCustom Office Templates" -PropertyType ExpandString -Force }
獲取注冊值
$regvalue = (Get-ItemPropertyValue 'HKCU:SOFTWAREMicrosoftOffice16.0WordOptions') 'PersonalTemplates')
uj5u.com熱心網友回復:
我已經把你的代碼片段按順序整理好了,也糾正了檢查注冊表鍵是否存在的邏輯。
ForEach ($user in (Get-ChildItem "C: Users" -Exclude Public>)
{
$location = "C:Users$($user.Name)DocumentsCustom Office Templates"/span>
$IsPresent = Get-ItemProperty 'HKCU:SOFTWAREMicrosoftOffice16. 0WordOptions'|ForEach-Object {If($_ -like '*PersonalTemplates*'){ Return 'True' }}.
if(-Not($IsPresent) -eq 'True')
{
New-ItemProperty -Path 'HKCU:SOFTWAREMicrosoftOffice16. 0WordOptions' -Name 'PersonalTemplates' -Value $location -PropertyType ExpandString -Force Not tested
New-Item -ItemType Directory -Force -Path $location
}
$existingValue= Get-ItemPropertyValue -Path 'HKCU:SOFTWAREMicrosoftOffice16.0WordOptions'/span> -Name 'PersonalTemplates'/span>
if([string]::IsNullOrWhiteSpace($existingValue)){
Set-ItemProperty -Path 'HKCU:SOFTWAREMicrosoftOffice16.0WordOptions' -Name 'PersonalTemplates' -Value $location
}
else{
$location=$existingValue else{
if(! (test-path $existingValue)
{
New-Item -ItemType Directory -Force -Path -$existingValue。
}
}
Copy-Item template.dotm -Destination $location }
}
我沒有測驗注冊表鍵的創建,因為我在作業的筆記本電腦上,所以假設這行代碼是有效的。
另外,我要問你一個問題。有了這種方法,注冊表項就不會有第一個用戶檔案夾的單一值了,你可能要研究一下這個邏輯?我覺得你可能需要在每個用戶登錄后使用$env:Username為他們運行這個腳本,而不是在用戶檔案夾中回圈。但我可能是錯的,可能有其他的人可以提出更好的建議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/316128.html
標籤:
