0

When i use this:

"$LogTime $CurrentDate Invalid Archive Grouping selected. You selected '$ArchiveGrouping'" | Out-File $LogFile -Append -Force 

To write to a file i generated with this code:

$Logdate = Get-Date
$Logname = $Logdate.ToString("yyyyMMdd") + ".txt"
Add-Content -Path C:\ArchiveStorage\$Logname "Log" -force
$LogFile = "C:\ArchiveStorage\$Logname"

The Text in the file looks weird it looks like this: enter image description here

if i change the code to just write to an existing file like:

$LogFile = "C:\ArchiveStorage\Log.txt"

The text file is as it should be: enter image description here

why does it make spaces and all that random crap ?

Metal Mike
  • 89
  • 4
  • 12

2 Answers2

3

You've been hit by double byte Unicode. When, say, letter A is written into the file, there usually is a single byte value 0x41. In double byte Unicode, the byte value for A is 0x0041 or 0x4100 depending on endianess.

When Notepad opens a file that has no Unicode byte order mark, it assumes all the extra 00 in file contents are blank spaces. That's why you do see w e r i d s p a c i n g.

For a fix, try using -Encoding ASCII parameter with Add-Content.

vonPryz
  • 19,131
  • 6
  • 48
  • 56
2

The "Log" entries look different because they were written with Add-Content, which uses a default encoding of ASCII, and Out-File uses a default encoding of Unicode.

Either specify the encoding type on Out-File, or switch to using Add-Content for both the "Log" heading and the detail lines.

mjolinor
  • 59,504
  • 6
  • 99
  • 125