前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >powershell重定向文本文件时注意显性指定编码

powershell重定向文本文件时注意显性指定编码

原创
作者头像
Windows技术交流
修改2023-04-14 15:18:57
1.5K0
修改2023-04-14 15:18:57
举报
文章被收录于专栏:Windows技术交流Windows技术交流

powershell编码,如果想生成的文件名和文件内容中的中文正常

看下https://til.secretgeek.net/powershell/out-file-encoding.html

-encoding ascii

-encoding default

-encoding oem

-encoding utf7

安装个notepad++(https://github.com/notepad-plus-plus/notepad-plus-plus),powershell执行下面代码后,用notepad++挨个打开去看,上面列出的4种是utf-8,我一般用-encoding ascii,尤其是.bat、.cmd、.vbs这些可执行文件,编码一定要显性指定-encoding ascii

"unknown" | out-file "c:\out-file-unknown.txt" -encoding unknown

"string" | out-file "c:\out-file-string.txt" -encoding string

"unicode" | out-file "c:\out-file-unicode.txt" -encoding unicode

"bigendianunicode" | out-file "c:\out-file-bigendianunicode.txt" -encoding bigendianunicode

"utf8" | out-file "c:\out-file-utf8.txt" -encoding utf8

"utf7" | out-file "c:\out-file-utf7.txt" -encoding utf7

"utf32" | out-file "c:\out-file-utf32.txt" -encoding utf32

"ascii" | out-file "c:\out-file-ascii.txt" -encoding ascii

"default" | out-file "c:\out-file-default.txt" -encoding default

"oem" | out-file "c:\out-file-oem.txt" -encoding oem

如果是记事本编辑中文,保存的时候选择ANSI

.ps1里尽量不要用中文,要用就把中文转成Unicode编码去写代码

比如下面这种不可取

代码语言:javascript
复制
if ($Disk -match 'Disk%s+(?<DiskIdx>%d+)%s+(Online|Offline)%s+(?<Size>%d+)%s+GB%s+(?<Free>%d+)|磁盘%s+(?<DiskIdx>%d+)%s+(联机|脱机)%s+(?<Size>%d+)%s+GB%s+(?<Free>%d+)|Disk%s+(?<DiskIdx>%d+)%s+(Online|Offline)%s+(?<Size>%d+)%s+TB%s+(?<Free>%d+)|磁盘%s+(?<DiskIdx>%d+)%s+(联机|脱机)%s+(?<Size>%d+)%s+TB%s+(?<Free>%d+)')
{

}

应该是

代码语言:javascript
复制
if ($Disk -match 'Disk\s+(?<DiskIdx>\d+)\s+(Online|Offline)\s+(?<Size>\d+)\s+GB\s+(?<Free>\d+)|\u78c1\u76d8\s+(?<DiskIdx>\d+)\s+(\u8054\u673a|\u8131\u673a)\s+(?<Size>\d+)\s+GB\s+(?<Free>\d+)|Disk\s+(?<DiskIdx>\d+)\s+(Online|Offline)\s+(?<Size>\d+)\s+TB\s+(?<Free>\d+)|\u78c1\u76d8\s+(?<DiskIdx>\d+)\s+(\u8054\u673a|\u8131\u673a)\s+(?<Size>\d+)\s+TB\s+(?<Free>\d+)')
{

}

微软资料:https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_character_encoding

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • powershell编码,如果想生成的文件名和文件内容中的中文正常
  • 如果是记事本编辑中文,保存的时候选择ANSI
  • .ps1里尽量不要用中文,要用就把中文转成Unicode编码去写代码
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档