jeffharbert

Create Word Document with Powershell

Oct 22nd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This Powershell script just creates a new, empty Word document and saves it.
  2. # Potentially useful for batch operations or for getting started on
  3. # a more complex script.
  4.  
  5. # Create an instance of Word the script will work with
  6. $objWord = New-Object -comobject Word.Application
  7.  
  8. # Set Word to be visible on screen.
  9. # This can also be set to $False to make Word not appear
  10. # on screen but just run in the background.
  11. $objWord.Visible = $True
  12.  
  13. # Now create a new empty document.
  14. $objDoc = $objWord.Documents.Add()
  15.  
  16. # Steps to add content to the document will go here.
  17. # For now we're just saving a new, blank document.
  18.  
  19. # Assign a static file name and path to a variable.
  20. $filename = "C:\scripts\temp.docx"
  21.  
  22. # Save the Word document using the variable we just created.
  23. $objDoc.SaveAs([REF]$filename)
  24.  
  25. # This step will print the document to the default
  26. # printer using its default settings.
  27. # Uncomment the line if you want the document to print.
  28. #$objDoc.PrintOut()
  29.  
  30. # Close the document.
  31. $objDoc.Close()
  32.  
  33. # Quit Word.
  34. $objWord.Quit()
Advertisement
Add Comment
Please, Sign In to add comment