Powershell Scripting Games - Day 7

Here is my solution for the Beginner Event 7

Here we had to create a logging solution for script

# ------------------------------------------------------------------------
# NAME: ReplaceWordInWord.ps1
# ------------------------------------------------------------------------

$mypath = "c:\temp\test.doc"
$logfile = "c:\temp\logfile.txt"
#first we will see if the logfile exists
$exists = Test-Path c:\temp\logfile.txt

    if ($exists -eq $true) {
        Remove-Item C:\temp\logfile.txt
    }
    New-Item -path c:\temp\ -Name logfile.txt -type "file" #Create the logfile
  
"$(get-date) -- Created Log file: $logfile" | Out-File ` 
    -Encoding ascii -Append $logfile
  
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
"$(get-date) -- We have launched word" | Out-File ` 
    -Encoding ascii -Append $logfile
  
$objDoc = $objWord.Documents.Open($mypath)
"$(get-date) -- We have the document" | Out-File ` 
    -Encoding ascii -Append $logfile
  
$objSelection = $objWord.Selection
$FindText = "mispelled"
$ReplaceText = "spelled incorrectly"
"$(get-date) -- Replacing '$FindText' with '$ReplaceText'" | Out-File ` 
    -Encoding ascii -Append $logfile

$ReplaceAll = 2
$FindContinue = 1
$MatchCase = $False
$MatchWholeWord = $True
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $False

"$(get-date) -- Executing command" | Out-File -Encoding ascii -Append $logfile

$objSelection.Find.Execute($FindText,$MatchCase,
  $MatchWholeWord,$MatchWildcards,$MatchSoundsLike,
  $MatchAllWordForms,$Forward,$Wrap,$Format,
  $ReplaceText,$ReplaceAll)

"$(get-date) -- Replace text complete" | Out-File ` 
    -Encoding ascii -Append $logfile