Powershell 日常

发布时间 2023-04-26 23:31:10作者: UPeRVv

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 " "
}

0x04 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>

更多

  1. 用新PowerShell 运行旧的CMD命令 - CSDN

参考

  1. microsoft.powershell.core > about_profiles
  2. microsoft.powershell.core > about_environment_variables
  3. microsoft.powershell.core > about_prompts
  4. lang-spec > chapter-15 Grammar