Thursday, November 20, 2025

How to automatically start word and type in it with Powershell

 

#Note This first line is mandatory 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")


# Start WinWord (if not already running)

Start-Process -FilePath "winword.exe"


# Wait for WinWord to open and become active

Start-Sleep -Seconds 3


# Activate the WinWord window (adjust the title if necessary)

$wshell = New-Object -ComObject wscript.shell

#$wshell.AppActivate("Document1 - Word") # Or the actual document title

Start-Sleep -Seconds 1


# Send some text and an Enter key

[System.Windows.Forms.SendKeys]::SendWait("This is a test message.")

[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

[System.Windows.Forms.SendKeys]::SendWait("Another line of text.")


No comments:

Post a Comment