2016-11-23 59 views
-1

這個問題不應該在這裏懸而未決困境

+0

'[控制檯] :: OutputEncoding = [Text.Encoding] :: UTF8'或'開始處理-RedirectStandardOutput' – PetSerAl

+2

那麼你應該張貼一些代碼,或者更好[MCVE。有很多地方可能會出錯,但沒有任何人只能猜測的代碼。 – PetSerAl

+0

請編輯您的問題,而不是在評論中張貼代碼片段。按照@ PetSerAl的評論,發佈[mcve]。 – JosefZ

回答

1

默認編碼>Redirection以及爲Out-File是Unicode:

Out-File cmdlet可以將輸出發送到一個文件中。當你需要使用它的 參數您可以使用此cmdlet 而不是重定向運算符(>)。

-Encoding

指定文件中使用的字符編碼的類型。有效 值是 「統一」, 「UTF7」, 「UTF8」, 「UTF32」, 「ASCII」, 「BigEndianUnicode」, 「默認」 和 「OEM」。
「統一」是默認

「默認」使用系統當前ANSI代碼頁的編碼。

「OEM」使用操作系統的當前 原始設備製造商代碼頁標識符。

示例腳本

'' 
'$outputencoding>$env:TEMP\40763209.txt' 
$outputencoding>$env:TEMP\40763209.txt 
(Get-Content -Path $env:TEMP\40763209.txt -Encoding Byte -TotalCount 16 | 
    ForEach-Object {"{0:x2}" -f $_}) -join ' ' 
'' 
'$outputencoding|out-file $env:TEMP\40763209a.txt -Encoding utf8' 
$outputencoding|out-file $env:TEMP\40763209a.txt -Encoding utf8 
(Get-Content -Path $env:TEMP\40763209a.txt -Encoding Byte -TotalCount 16 | 
    ForEach-Object {"{0:x2}" -f $_}) -join ' ' 

輸出:注意這兩個文件Byte Order Mark簽名:

PS D:\PShell> D:\PShell\SO\40763209.ps1 

$outputencoding>$env:TEMP\40763209.txt 
ff fe 0d 00 0a 00 0d 00 0a 00 49 00 73 00 53 00 

$outputencoding|out-file $env:TEMP\40763209a.txt -Encoding utf8 
ef bb bf 0d 0a 0d 0a 49 73 53 69 6e 67 6c 65 42 
+0

我的問題正是在這一步之後:我能夠通過簡單地打開帶有重定向輸出的文本文件並在記事本中單擊「另存爲...」來識別編碼。問題是,將文件內容轉換爲** any **其他編碼會導致相同的情況:每個字符之間有空格,在某些情況下甚至有兩個空格。 避免材料清單是我已經嘗試通過閱讀許多類似於我的網上類似問題。 – REMOVED