Talen Kylon
Talen Kylon

Reputation: 1968

Weird new line behavior between Windows 7 Notepad and Windows 2012 Server Notepad

I've whipped up a script that deletes old video directories, and it will create a log of what has been deleted in a .txt file.

Running the script on my Windows 7 PC, the .txt file in Notepad puts each output on a new line.

Running the script on my Windows 2012 Server, the .txt file in Notepad puts the output on the same line. If I open the log file with Wordpad, the outputs are on new lines.

Here's what the powershell script looks like:

$limit = (Get-Date).AddDays(0)
$logFile = "C:\BodyCam\bodycamlog.txt"
$output = "The following directories contain incorrect video dates" + "`n"

#Throw a timestamp on the log file
Add-Content -Path $logFile "=======$(Get-Date)======="

#Get all of the paths for each camera
$paths = Get-ChildItem -Path "D:\Videos\" |Select-Object -ExpandProperty FullName

#for each path we found
foreach ($pa in $paths) {
    # Delete directories older than the $limit.
    $dir = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } 
    $dir | Remove-Item -Recurse -Force
    $dir | Select -Expand FullName | Out-File $logFile -append
    if ($dir -match "1970"){
        $output += $pa + "`n"
    }
}
Send-MailMessage -to "Ray <***>" -from "BodyCamScript <***>" -subject "Bad Bodycam Date Found" -body $output -SmtpServer ***

What's the deal here?

Upvotes: 0

Views: 77

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40818

The default encoding for Add-Content is ASCII, but the default for Out-File is Unicode. Use the encoding parameter on both to force the same encoding and it should work as you expect.

Upvotes: 3

Related Questions