Get and Set Microsoft Word Custom Document Properties

Here’s a quick snippet that details how to get and set custom document properties in Microsoft Word 2010 documents:

#Create Word application
$wordApplication = New-Object -ComObject word.application

#Get reference to word doc
$document = $"C:\temp\alkane.docx");

#set up binding flags for custom properties
$binding = "System.Reflection.BindingFlags" -as [type];        
$customProperties = $document.CustomDocumentProperties

[Array]$propertyName = "ExampleProperty"
[Array]$propertyValue = "Example Value"

#Get property value
$myProperty = [System.__ComObject].InvokeMember("Item", $binding::GetProperty, $null, $customProperties, $propertyName)
$myPropertyValue  = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$myProperty,$null);

#Set property value

#Update all fields
$document.Fields.Update() | Out-Null

#save and close document

#required for O365
$document.Saved = $false;