Skip to content

Instantly share code, notes, and snippets.

@eugrus
Created September 18, 2024 08:34
Show Gist options
  • Save eugrus/cab02a202935f5bc9ecf329a3315a868 to your computer and use it in GitHub Desktop.
Save eugrus/cab02a202935f5bc9ecf329a3315a868 to your computer and use it in GitHub Desktop.
# Get the file path
param(
[string]$filePath
)
$filePath = Resolve-Path -Path $filePath
# Ensure the file exists
if (-not (Test-Path $filePath)) {
Write-Error "File does not exist: $filePath"
exit 1
}
# Create a new Word Application COM object
$word = New-Object -ComObject Word.Application
# Set Word to be hidden
$word.Visible = $false
# Open the document
$document = $word.Documents.Open($filePath)
# Read the content
$content = $document.Content.Text
# Close the document without saving
$document.Close([ref]$false)
# Quit Word
$word.Quit()
# Output the content
Write-Output $content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment