CodexBloom - Programming Q&A Platform

PowerShell 7.3 - Trouble with Get-Content and File Encoding When Reading Log Files

πŸ‘€ Views: 15 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-10
powershell encoding get-content

I'm refactoring my project and I'm working with an scenario while trying to read log files using `Get-Content` in PowerShell 7.3. The log files are encoded in UTF-16, but when I attempt to read them, I get garbled output. I've tried specifying the encoding explicitly, but it seems ineffective. Here’s the command I used: ```powershell $logContent = Get-Content -Path 'C:\Logs\myLogFile.log' -Encoding Unicode ``` Instead of the expected content, I see strange characters, like `ΓΏΓΏ` at the beginning and other unexpected symbols throughout. I also tried using `-Raw` to get the entire content in one go: ```powershell $logContent = Get-Content -Path 'C:\Logs\myLogFile.log' -Encoding Unicode -Raw ``` But the output still contains those strange characters. I confirmed that the log file is indeed UTF-16 by checking it in a text editor that displays the encoding. I've also checked other PowerShell commands like `Out-File` using the same encoding, and they seem to work fine. Is there a different approach I should take to read a UTF-16 encoded file without getting garbled text? Any insights would be appreciated! Is this even possible?