Powershell Output double spacing

I have some simple code that will output to a log file. IF I run it within Visual Studio Code, output is perfect. However, if I schedule the script to run via Task Scheduler, the script runs fine; however, the output in the log file is double space between characters and between lines. The code will simply check for contents of a folder. Snippet of the output code and result is below:

If $folder is empty:
Write-Output “$(Get-TimeStamp) $folder is empty” >> $LogCheck

If folder is not empty:
Write-Output “$(Get-TimeStamp) $folder is not empty” >> $LogCheck

If I run with Visual Studio Code:
$folder is empty
[03/08/23 08:36:34] $Folder is Empty
[03/08/23 08:41:34] $Folder is Empty
[03/08/23 08:41:34] $Folder is not Empty

If I run it using TaskScheduler (Simply creating schedule and trigger, running Powershell.exe with the script as an argument.)

[ 0 3 / 0 8 / 2 3 1 0 : 2 1 : 3 4 ] $ F o l d e r i s E m p t y

[ 0 3 / 0 8 / 2 3 1 0 : 2 6 : 3 4 ] $ F o l d e r i s E m p t y

[ 0 3 / 0 8 / 2 3 1 0 : 3 1 : 3 4 ] $ F o l d e r i s E m p t y

[ 0 3 / 0 8 / 2 3 1 0 : 3 6 : 3 4 ] $ F o l d e r i s N o t E m p t y

[ 0 3 / 0 8 / 2 3 1 0 : 4 1 : 3 4 ] $ F o l d e r i s N o t E m p t y

Take a look at this:

about Redirection - PowerShell | Microsoft Learn

Specifically, the Notes:

When you are writing to files, the redirection operators use UTF8NoBOM encoding. If the file has a different encoding, the output might not be formatted correctly. To write to files with a different encoding, use the Out-File cmdlet with its Encoding parameter.

Here is another link discussing options for external logging:

active directory - powershell Write-Output with >> to a file.txt -Append - Stack Overflow

Thanks for the reply Rob; I will look into these. If these are a solution, I am just confused why the output is correct if I run the code via Visual Studio Code (and now, I have tried it simply via command line, and it outputs correctly). It just has the skewed output when launched via Scheduler.

I dont know how youre formatting your task, but you can try like this. Just in case anything is in that argument line that is causing it.

In the Action tab;

Program/script:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments:
-file “DIR:\PATH\script.ps1”

It sounds like the issue is related to the encoding used by Task Scheduler when running the script. By default, Task Scheduler runs scripts with the system default encoding, which can be different from the encoding used by Visual Studio Code.

To fix the issue, you can try setting the encoding explicitly in your script. Here’s an example:

# Set the encoding to UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

# Write output to log file
Write-Output "$(Get-TimeStamp) $folder is empty" >> $LogCheck

This will ensure that the output is written to the log file using the UTF8 encoding, which should be consistent across both Visual Studio Code and Task Scheduler.

Alternatively, you can try changing the system default encoding to match the encoding used by Visual Studio Code. You can do this by setting the $PSDefaultParameterValues variable in your script, like this:

# Set the default encoding to UTF8
$PSDefaultParameterValues['*-Encoding'] = 'UTF8'

# Write output to log file
Write-Output "$(Get-TimeStamp) $folder is empty" >> $LogCheck

This will set the default encoding for all cmdlets that have an -Encoding parameter to UTF8, which should ensure that the output is consistent across both Visual Studio Code and Task Scheduler.