1

I need to get count of character in word document via power shell ? it is any way to do it ? I have some code like this but I suggest it is could be way without Paragraphs

     $Word = New-Object -comobject Word.Application
    $Word.Visible = $False
    $datasheet = $word.Documents.Open("d:\Test words.docx")
    
    
    $FirstParagraph = $datasheet.Paragraphs[1].range.Text
    $FirstParagraph | measure -Line -Character -Word

$datasheet.close()
$word.Quit()

1 Answer 1

1

try this :

$Word = New-Object -comobject Word.Application
$Word.Visible = $False
$datasheet = $word.Documents.Open("c:\temp\test.docx")
$Word.ActiveDocument.Content.Select()

$NbCharacter=$Word.ActiveDocument.Characters.Count.ToString()

"File has $NbCharacter charactere"

$datasheet.close()
$word.Quit()
Sign up to request clarification or add additional context in comments.

4 Comments

thanks working well, but I have small question word directly shows 48 character for example, but script 56. I understand it is some hidden character, it is anyway to get value what word shows in UI ?
your script have endilne and tabulation character i think
do you mean document ? how to skip them ?
vba counts things that the Word Interface does not. When checking for "words" I found a ratio of about 1.13:1. If you check the ratio for characters, you can get a very good approximation through vba by reducing the result to reflect that ratio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.