Jason
Jason

Reputation: 93

Weird character in windows powershell output file

My OS is win10, and powershell version is 7.10.pre6.

I used ls > D:\test.txt to list the files and wrote to a file. But I found the "Name" column had some weird characters surrounding it, as picture shows:

snapshot of the output file

I don't know why. And when I used command like grep, awk...it turned the chinese character into messy code. For example, ls | grep -i system would result in:

la---       2020/5/20   涓婂崍11:20        1386296 AppVEntSubsystemController.dll
la---       2020/6/10   涓嬪崍10:29        2190648 AppVEntSubsystems64.dll
la---       2020/8/25    涓嬪崍2:07         463168 microsoft-windows-system-events.dll
la---       2019/3/19   涓嬪崍12:44          95744 systemcpl.dll
la---       2019/3/19   涓嬪崍12:45         102912 systeminfo.exe
la---       2020/3/12    涓嬪崍1:46         510768 systemreset.exe

Is that a BUG of powershell? Or is there any way to set the charset? By the way, I tried chcp 65001, and that didn't work.
Any help would be appreciated.
Thanks.

Upvotes: 2

Views: 1948

Answers (1)

filimonic
filimonic

Reputation: 4634

It's not bug. Those characters are ANSI color codes, that's because you redirect raw colored output incorrectly. Operator > redirects text output, including color codes.

To export data try using ls | Select FullName, CreationTime | Out-File C:\data.txt or ls | Export-Csv ...

Remember, in PowerShell everything is an object, not a string.

Upvotes: 1

Related Questions