How to specify encoding when creating Powershell text file ...

文章推薦指數: 80 %
投票人數:10人

I'm having an issue where the file is saved with encoding type Unicode - whereas I want the file to be saved as ANSI . This is a Powershell ... Searchsqlserver-dba.com Followsqlserver-dba.com   *UsetheCommentssectionforquestions Daily&ExclusiveContent SQLServer-DBA.comLinks RecentPosts HowtoenableSQLServertraceflagsatstartup SQLServeruserrolesandpermissionsquery ListForeignKeysreferencingtablesinSQLServer DoesAWSRDSSQLServersupportDynamicManagementViews? WhatishorizontalpartitioninginSQLServer? ReasonsforTransactionLogBackupChainbreaking HashicorpVaultcurlforVBA Column-levelencryptionvsAlwaysencrypted Thesimpleguidetocolumn-levelencryption HelpwithPowershellfunctions SQLServerDBAJobs HowtospecifyencodingwhencreatingPowershelltextfileusingOut-file 02October,2019by TomCollins Question:I'vedeployedaPowershellscript-whichincludesaprocessto dumpoutaresultsettoacsvfile.I'mhavinganissuewherethefileissavedwithencodingtypeUnicode-whereasIwantthefiletobesavedasANSI.ThisisaPowershellcodesnippet,usingthePowershellcmdletOut-File    (some_input)|select*-ExcludePropertyRowError,RowState,HasErrors,Name,Table,ItemArray|ConvertTo-Csv-NoTypeInformation|Select-Object-Skip1|Out-File-filepath myoutputfile.txt WhenI openthefileinNotepad-andattemptaSaveAs-theencodingdisplaysasUnicode.     HowcanIforcePowershelltoencodeasANSI?   Answer:  It'spossibletoforcetheEncodingtypethroughextending the useofPowershellcmdletout-file.Thereisaparametercalled-Encoding.ThisistheadjustedPowershellcodesnippetyouoriginally posted.Theadjustedcodesnippetincludestheadditionofthe-EncodingparameteraspartofOut-File.  TheinputdataispipedandaConvertTo-Csvisexecuted,pipedtoaSelectwhichskipsthefirstrow,andthenpipedtoanOut-File,includingtheexplicitEncoding     (some_input)|select*-ExcludePropertyRowError,RowState,HasErrors,Name,Table,ItemArray|ConvertTo-Csv-NoTypeInformation|Select-Object-Skip1|Out-File-filepath -EncodingAsciimyoutputfile.txt lfyouexecuteyourcodeagain-andopeninNotepad-youshouldnowseeANSIastheEncodingtype LetmeknowifthisworksoranyothermethodsyoufoundtomanagetheEncodingtype  ReadmoreonPowershellandcsvmanagement  Export-CSVPowershell (SQLServerDBA) Howtowriteremove-itemresultstotextwith powershell out-file Powershell writetoWorddocument(SQLServerDBA) Author:TomCollins(http://www.sqlserver-dba.com) Share: PostedbyTomCollinsat9:18AM inPowershell | Permalink | Comments(0) VerifyyourComment PreviewingyourComment Postedby: | Thisisonlyapreview.Yourcommenthasnotyetbeenposted. Yourcommentcouldnotbeposted.Errortype: Yourcommenthasbeenposted.Postanothercomment Thelettersandnumbersyouentereddidnotmatchtheimage.Pleasetryagain. Asafinalstepbeforepostingyourcomment,enterthelettersandnumbersyouseeintheimagebelow.Thispreventsautomatedprogramsfrompostingcomments. Havingtroublereadingthisimage?Viewanalternate. PostacommentonHowtospecifyencodingwhencreatingPowershelltextfileusingOut-file (URLsautomaticallylinked.) YourInformation (Nameandemailaddressarerequired.Emailaddresswillnotbedisplayedwiththecomment.) Nameisrequiredtopostacomment Pleaseenteravalidemailaddress InvalidURL sqlserver-dba.com|SQLServerPerformanceTuning|SQLServerDBA:Everything|FAQ|Contact|Copyright&Disclaimer



請為這篇文章評分?