Can't get/set Word's "Publish Date" with PowerShell. What am I doing it wrong? RRS feed

  • Question

  • Hello,

    After reading several blogs, posts and documentation I couldn't find the solution to my problem. Going to summarize in some detail.

    Script purpose: update publish date in several word documents using PowerShell

    Fact: All documents use "Quick Parts->Document Properties | Publish Date".

    Problem: After I defined the property to change "Publish Date", it gave an error on getting property type.

    What have I done so far: I found a code that listed all the properties and custom properties, although, none of them listed "Publish Date". Adding some fact to the pot, neither "Abstract" was listed and yet is listed in the next printscreen I attach as a native Word quick part.

    Additional info: One thing that I also checked was if for some reason the "Publish Date" in my document's template was defined incorrectly or Word couldn't list properties... I click on Properties in the "Developer" tab in Word and it was listed.

    So, basically, after some hours trying several approaches, none worked out. Can anybody help me or explain why is not "Publish Date" available or I'm just looking in the wrong spot? :)

    Here's the code I'm got it from stackoverflow and just changed some things)

    $propertiesToUpdate = @{
        "Pubish Date" = "01/01/2018"
    #Update the path to the documents to update:
    $path = ".\*.docx"
    Write-Host -ForegroundColor Cyan "Loading Application..."
    $application = New-Object -ComObject word.application
    $application.Visible = $false
    function AddOrUpdateCustomProperty ($CustomPropertyName, $CustomPropertyValue, $DocumentToChange)
        $customProperties = $DocumentToChange.CustomDocumentProperties
        $typeCustomProperties = $customProperties.GetType()
        $binding = "System.Reflection.BindingFlags" -as [type]
        [array]$arrayArgs = $CustomPropertyName,$false, 4, $CustomPropertyValue
            $typeCustomProperties.InvokeMember("add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) | out-null
        Catch [system.exception] 
            $propertyObject = $typeCustomProperties.InvokeMember("Item", $binding::GetProperty, $null, $customProperties, $CustomPropertyName)
            $typeCustomProperties.InvokeMember("Delete", $binding::InvokeMethod, $null, $propertyObject, $null)
            $typeCustomProperties.InvokeMember("add", $binding::InvokeMethod, $null, $customProperties, $arrayArgs) | Out-Null
        Write-Host -ForegroundColor Green "Success! Custom Property:" $CustomPropertyName "set to value:" $CustomPropertyValue
    ForEach($File in (GCI $path|Select -Expand FullName))
        Write-Host -ForegroundColor Cyan "Opening Document..." $File
        $document = $application.documents.open($File)
        ForEach($property in $propertiesToUpdate.GetEnumerator())
            AddOrUpdateCustomProperty $($property.Name) $($property.Value) $document
        Write-Host -ForegroundColor Cyan "Updating document fields."
        $document.Fields.Update() | Out-Null
        Write-Host -ForegroundColor Cyan "Saving document."
        $document.Saved = $false
    $application = $null
    Write-Host -ForegroundColor Green "Done!"

    • Edited by JEstevao Thursday, April 12, 2018 5:34 PM
    Thursday, April 12, 2018 5:32 PM


  • The "Publish Date" is a Content Control and has to be inserted into the document.

    Here is how to get the inserted control:

    PS D:\scripts> $x = $document.ContentControls|?{$_.Title -eq 'Publish Date'}
    PS D:\scripts> $x.Range.Text
    PS D:\scripts>$x.Range.Text = '5/1/2017'  # to set the value


    Thursday, April 12, 2018 6:27 PM