Created
September 18, 2024 08:34
-
-
Save eugrus/cab02a202935f5bc9ecf329a3315a868 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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