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.
延伸文章資訊
- 1Understanding file encoding in VS Code and PowerShell
This problem occurs because VS Code encodes the character – in UTF-8 as the bytes 0xE2 0x80 0x93 ...
- 2Change / Save encoding How to convert several txt files UTF ...
Hello. I need to convert several txt files , located in C:\Folder1 from UTF-8 to UTF-8-BOM. I had...
- 3Read 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...
- 4Get Text File Encoding - Power Tips - IDERA Community
That's why most cmdlets dealing with text file reading offer the ... Encoding]::UTF8} [PSCustomOb...
- 5Changing source files encoding and some fun with PowerShell
Changing source files encoding and some fun with PowerShell ... At least it can correctly read te...