Powershell keeps converting to ascii

2019-09-01 02:53发布

I've followed the guide here: Use Windows PowerShell to Look for and Replace a Word in a Microsoft Word Document

My problem is that if I put a UTF8 string into the search filed it gets converted to ASCII before going into the word application.

If you simply copy and paste his code and change the find text to something like Japanese: カルシウム

It will go into work and search for the ascii equivalent: カルシウム

I have tried every suggestion about setting input and output to UTF8 that I can find but nothing seems to be working. I can't even get the powershell console to actually display Japanese characters, all I get are boxes. I think that might have something to do with the fact that I only have 3 fonts and perhaps none of them can display the Japanese characters in the console...but I don't care about that, I want to be able to send the Japanese characters in UTF8 for the find and replace.

Any Help?

2条回答
聊天终结者
2楼-- · 2019-09-01 03:02

The answer is actually quite easy. If the powershell-script is saved as UTF8, characters are not encoded correctly. You'd need to save the ps1-script encoded as "UTF8 with BOM" in order to get the characters right.

查看更多
劳资没心,怎么记你
3楼-- · 2019-09-01 03:28

For people who keep getting output encoding to ASCII or Unicode all the time, you can set output encoding to whatever encoding you want from Microsoft blog $OutputEncoding

  1. PS C:\> $OutputEncoding //tells you what default encoding you have
  2. PS C:\> $OutputEncoding = [Console]::OutputEncoding //change to the console or you can set it
  3. $OutputEncoding = New-Object -typename System.Text.UTF8Encoding //whatever you want, look up japanese
  4. PS C:\> $OutputEncoding // verify the encoding
查看更多
登录 后发表回答