Get Text File Encoding - Power Tips - IDERA Community
文章推薦指數: 80 %
That's why most cmdlets dealing with text file reading offer the ... Encoding]::UTF8} [PSCustomObject]@{ Encoding = $enc Path = $Path } } }. Home Blog PerformanceMonitoring&Tuning SQLDiagnosticManagerforSQLServer SQLDefragManager SQLDiagnosticManagerforMySQL SQLDoctor SQLQueryTunerforSQLServer SQLWorkloadAnalysisforSQLServer Precise UptimeInfrastructureMonitor DatabaseAdministration SQLAdminToolset SQLEnterpriseJobManager SQLInventoryManager SQLSafeBackup DatabaseSecurity SQLComplianceManager SQLSecure DataModeling ER/StudioBusinessArchitect ER/StudioDataArchitect ER/StudioTeamServer DatabaseDevelopment&Management AquaDataStudio DBPowerStudio PowerShell PowerTips AsktheExperts UsingPowerShell VideoLibrary ScriptLibrary ModuleLibrary Events GeekSyncs LiveDemos FreeTools More Cancel DatabaseTools PowerShell PowerTips More Cancel PowerTips PowerTips GetTextFileEncoding Blogs Mentions Sub-Groups Tags More Cancel New GetTextFileEncoding Share Contactauthor Subscribebyemail More Cancel Related Recommended ps1 22Jan2019 Textfilescanbestoredusingdifferentencodings,andtocorrectlyreadingthem,youmustspecifytheencoding.That’swhymostcmdletsdealingwithtextfilereadingofferthe-Encodingparameter(forexample,Get-Content).Ifyoudon’tspecifythecorrectencoding,youarelikelyendingupwithmessedupspecialcharactersandumlauts. Yethowdoyou(automatically)determinetheencodingagiventextfileuses?Hereisahandyfunctionthatcanhelp: functionGet-Encoding { param ( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias('FullName')] [string] $Path ) process { $bom=New-Object-TypeNameSystem.Byte[](4) $file=New-ObjectSystem.IO.FileStream($Path,'Open','Read') $null=$file.Read($bom,0,4) $file.Close() $file.Dispose() $enc=[Text.Encoding]::ASCII if($bom[0]-eq0x2b-and$bom[1]-eq0x2f-and$bom[2]-eq0x76) {$enc=[Text.Encoding]::UTF7} if($bom[0]-eq0xff-and$bom[1]-eq0xfe) {$enc=[Text.Encoding]::Unicode} if($bom[0]-eq0xfe-and$bom[1]-eq0xff) {$enc=[Text.Encoding]::BigEndianUnicode} if($bom[0]-eq0x00-and$bom[1]-eq0x00-and$bom[2]-eq0xfe-and$bom[3]-eq0xff) {$enc=[Text.Encoding]::UTF32} if($bom[0]-eq0xef-and$bom[1]-eq0xbb-and$bom[2]-eq0xbf) {$enc=[Text.Encoding]::UTF8} [PSCustomObject]@{ Encoding=$enc Path=$Path } } } Hereisatestruncheckingalltextfilesinyouruserprofile: PS>dir$home-Filter*.txt-Recurse|Get-Encoding EncodingPath ------------ System.Text.UnicodeEncodingC:\Users\tobwe\E006_psconfeu2019.txt System.Text.UnicodeEncodingC:\Users\tobwe\E009_psconfeu2019.txt System.Text.UnicodeEncodingC:\Users\tobwe\E027_psconfeu2019.txt System.Text.ASCIIEncodingC:\Users\tobwe\.nuget\packages\Aspose.Words\18.12.0\... System.Text.ASCIIEncodingC:\Users\tobwe\.vscode\extensions\ms-vscode.powers... System.Text.UTF8EncodingC:\Users\tobwe\.vscode\extensions\ms-vscode.powers... psconf.eu–PowerShellConferenceEU2019–June4-7,HannoverGermany–visitwww.psconf.euTherearen’ttoomanytrainingsaroundforexperiencedPowerShellscripterswhereyoureallystilllearnsomethingnew.Butthere’soneplaceyoudon’twanttomiss:PowerShellConferenceEU-with40renowninternationalspeakersincludingPowerShellteammembersandMVPs,plus350professionalandcreativePowerShellscripters.Registrationisopenatwww.psconf.eu,andthefull3-track4-daysagendabecomesavailablesoon.Onceayearit’sjustasmartmovetocometogether,updateknow-how,learnaboutsecurityandmitigations,andbringhomefreshideasandauthoritativeguidance.We’dsurelovetoseeandhearfromyou! ReTweetthisTip! PoweredbyIDERA DatabaseMonitoring-Learnabout SQLDiagnosticManager DatabaseModelingandGovernance -Learnabout ER/Studio DatabaseDevelopment-Learnabout AquaDataStudio Iderausescookiestoimproveuserexperience.Byusingourcommunityyouconsenttoallcookiesinaccordancewithour Cookiepolicy.
延伸文章資訊
- 1批次將所有Java 原始碼檔案快速轉成UTF-8 編碼的方法
NET Framework 提供的 System.IO.File 類別,才能成功儲存以 UTF-8 編碼的文字檔案。 請注意: 以下程式執行到PowerShell 6+ 版本將會導致檔案內容整個...
- 2PowerShell Studio tip: View and change file encoding
- 3Get Text File Encoding - Power Tips - IDERA Community
That's why most cmdlets dealing with text file reading offer the ... Encoding]::UTF8} [PSCustomOb...
- 4Read UTF-8 files correctly with PowerShell - iTecNote
A PowerShell script creates a file with UTF-8 encoding; The user may or may not edit the file, po...
- 5Byte order mark - Globalization - Microsoft Learn