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.
延伸文章資訊
- 1PowerShell Studio tip: View and change file encoding
- 2Read UTF-8 files correctly with PowerShell - Stack Overflow
I need a function that can read any file with UTF-8 encoding, ignore and delete the BOM and not m...
- 3Set-Content - PowerShell - SS64.com
set-content -encoding UTF8 will write a BOM if one is available in the source file, or if the sou...
- 4Byte order mark - Globalization - Microsoft Learn
- 5Get Text File Encoding - Power Tips - IDERA Community
That's why most cmdlets dealing with text file reading offer the ... Encoding]::UTF8} [PSCustomOb...