Windows PowerShell
背景
Win11 21H2(OS Build 22000.1880)
Windows PowerShell 5.1
简述
0x01 环境变量
# list
PS> ls env:
# set
PS> $env:_JAVA_LAUNCHER_DEBUG=1
# unset
PS> del env:_JAVA_LAUNCHER_DEBUG
0x02 Profile
~ $ $profile
C:\Users\xxx\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
~ $ type $profile
# Truncate homedir to ~
function limit-HomeDirectory($Path) {
$Path.Replace("$home", "~")
}
# Must be called 'prompt' to be used by pwsh
function prompt {
$realLASTEXITCODE = $LASTEXITCODE
Write-Host $(limit-HomeDirectory("$pwd")) -ForegroundColor Yellow -NoNewline
Write-Host " $" -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
Return " "
}
0x03 Parsing
续行符 Line continuation,`
关于参数,有个要点,-
之后为参数名,参数名不能有 .
,会被断开传递,需要包引号。
command-parameter: dash first-parameter-char parameter-chars colon~opt~ parameter-chars: parameter-char parameter-chars parameter-char parameter-char: Any Unicode character except { } ( ) ; , | & . [ colon whitespace new-line-character
例:
$ .\TestExe.exe -echoargs -DA.D
Arg 0 is <-DA>
Arg 1 is <.D>
0x04 Character Encoding
In general, Windows PowerShell 5.1 uses the Unicode UTF-16LE encoding by default.
修改默认编码
$PSDefaultParameterValues
$OutputEncoding
- The automatic variable
$OutputEncoding
affects the encoding PowerShell uses to communicate with external programs. - It has no effect on the encoding that the output redirection operators and PowerShell cmdlets use to save to files.
- The automatic variable
例:
- $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
相关 Cmdlet
Beginning in PowerShell 5.1, the redirection operators (
>
and>>
) call theOut-File
cmdlet.
具体表现:
Out-File
and the redirection operators>
and>>
create UTF-16LE.Set-Content
andAdd-Content
useDefault
encoding.New-Item -Type File -Value
creates a BOM-less UTF-8 file.Get-Content
uses theDefault
ANSI encoding, in the absence of a BOM.
更多
参考
- microsoft.powershell.core > about_profiles
- microsoft.powershell.core > about_environment_variables
- microsoft.powershell.core > about_prompts
- microsoft.powershell.core > about_character_encoding
- lang-spec > chapter-15 Grammar
附录
BOM (byte-order-mark)[ref]
In Windows PowerShell, any Unicode encoding, except
UTF7
, always creates a BOM.PowerShell (v6 and higher) defaults to
utf8NoBOM
for all text output.
标签:
windows
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通