Removing Word custom properties
I was able to use the script to modify Word custom properties found here: 

http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/06/hey-scripting-guy-how-can-i-add-custom-properties-to-a-microsoft-word-document.aspx

I modified the script so that I could feed it a CSV file for multiple properties--it works great! (THANK YOU!)

Modified Script:

#Set the PATH variable to the location where you saved the script and CSV file
$path = "c:\temp\PowerShell Scripts\"

#Set the DOC variable to the location of the document you want to update
$doc = "c:\temp\test.docx"
$application = New-Object -ComObject word.application
$application.Visible = $false
$document = $application.documents.open($doc)
$binding = "System.Reflection.BindingFlags" -as [type]

$customProperties = $document.CustomDocumentProperties
$typeCustomProperties = $customProperties.GetType()

$CustomPropertiesWorklist = Import-Csv $path\args.csv
if($CustomPropertiesWorklist.Count){
for($i = 0; $i -lt $CustomPropertiesWorklist.Count; $i++)
	{
	$CustomProperty = $CustomPropertiesWorklist[$i].CP
	$msoPropertyType = $CustomPropertiesWorklist[$i].Type
	$Value = $CustomPropertiesWorklist[$i].Value
	
[array]$arrayArgs = $CustomProperty,$false,$msoPropertyType,$Value

Try
 {
  $typeCustomProperties.InvokeMember(`
    "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
    out-null
 }
Catch [system.exception]
 {
  $propertyObject = $typeCustomProperties.InvokeMember(`
    "Item", $binding::GetProperty,$null,$customProperties,$CustomProperty)
  $typeCustomProperties.InvokeMember(`
    "Delete", $binding::InvokeMethod,$null,$propertyObject,$null)
  $typeCustomProperties.InvokeMember(`
    "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
    Out-Null
 }
 }
 }
 else
 
 {
	$CustomProperty = $CustomPropertiesWorklist.CP
	$msoPropertyType = $CustomPropertiesWorklist.Type
	$Value = $CustomPropertiesWorklist.Value
	
[array]$arrayArgs = $CustomProperty,$false,$msoPropertyType,$Value

Try
 {
  $typeCustomProperties.InvokeMember(`
    "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
    out-null
 }
Catch [system.exception]
 {
  $propertyObject = $typeCustomProperties.InvokeMember(`
    "Item", $binding::GetProperty,$null,$customProperties,$CustomProperty)
  $typeCustomProperties.InvokeMember(`
    "Delete", $binding::InvokeMethod,$null,$propertyObject,$null)
  $typeCustomProperties.InvokeMember(`
    "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
    Out-Null
 }
 }
 
 
 
$document.Saved = $false
$document.save()
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
What I need now, however, is a way to REMOVE custom properties from a Word document (preferably using the same script).

Any ideas? 

July 30th, 2015 11:19am

Have you tried looking it up in the documentation?

Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 11:37am

Like this:
$typeCustomProperties.InvokeMember( "Delete", $binding::InvokeMethod,$null,$propertyObject,$null)
 
July 30th, 2015 11:58am

Sorry, I'm a PS newb... please bear with me...

That snippet is already in the script under the exception catch... It's used specifically to overwrite an existing property (per the link above). 

Catch [system.exception]
 {
  $propertyObject = $typeCustomProperties.InvokeMember(`
    "Item", $binding::GetProperty,$null,$customProperties,$CustomProperty)
  $typeCustomProperties.InvokeMember(`
    "Delete", $binding::InvokeMethod,$null,$propertyObject,$null)
  $typeCustomProperties.InvokeMember(`
    "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
    Out-Null
 }

How then do I incorporate this method when I want to fully delete a custom property? Perhaps when one of these is true?

A. The value is blank for a specific property, delete the property

or

B. Add another column (called 'DelCP') where '1' to keep and '0' to delete

Any nudge in the right direction would be helpful and greatly appreciated. 

Thanks!

Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 12:08pm

Just get it an delete it:

$propertyObject = $typeCustomProperties.InvokeMember('Item', $binding::GetProperty,$null,$customProperties,'CustomNumber2')
$typeCustomProperties.InvokeMember('Delete', $binding::InvokeMethod,$null,$propertyObject,$null)		
July 30th, 2015 12:10pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics