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 'Disks (?<DiskIdx>d )s (Online|Offline)s (?<Size>d )s GBs (?<Free>d )|u78c1u76d8s (?<DiskIdx>d )s (u8054u673a|u8131u673a)s (?<Size>d )s GBs (?<Free>d )|Disks (?<DiskIdx>d )s (Online|Offline)s (?<Size>d )s TBs (?<Free>d )|u78c1u76d8s (?<DiskIdx>d )s (u8054u673a|u8131u673a)s (?<Size>d )s TBs (?<Free>d )')
{
}
微软资料:https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_character_encoding