美化和自定义PowerShell界面

# 安装准备

  1. 安装 Windows Terminal (opens new window),或者使用微软应用商店
  2. 推荐安装 Caskaydia Cove Nerd Font 字体,在nerdfonts (opens new window)里查找,或者选择自己喜欢的字体,里面任意字体都可。
  3. 安装PowerShell (opens new window)
  4. [可选]安装VSCode (opens new window)

# 进行安装

首先安装Oh My Posh (opens new window) 根据官方教程进行安装:

Install-Module oh-my-posh -Scope CurrentUser
#(可选)主要方便于git功能
Install-Module posh-git -Scope CurrentUser
1
2
3

然后使用 记事本或 VSCode 来进行修改设置。

# 编辑PS配置文件(如无则创建该文件)
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
#记事本打开
notepad $PROFILE
#VSCode打开
code $PROFILE
1
2
3
4
5
6

最后在文件里加入下列指令:

Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme gmay
1
2
3

这样就会在 PowerShell 打开时,自动启动 oh-my-posh 并设置主题 (opens new window)gmay

注意

出现乱码现象,请把字体设置为 Caskaydia Cove Nerd 即可。

# 进阶设置

# 自定义主题

#命令行输入下即可查看内嵌主题
Get-PoshThemes
#选择自己喜欢的主题例如:
Set-PoshPrompt -Theme jandedobbeleer
1
2
3
4

首先新建个$.omp.json文件,可以根据如下配置文件进行修改。

查看配置文件
    {
      "blocks": [
        {
          "type": "prompt",
          "alignment": "left",
          "segments": [
            {
              "type": "os",
              "style": "diamond",
              "foreground": "#26C6DA",
              "background": "#546E7A",
              "properties": {
                "postfix": " \uE0B1",
                "macos": "mac"
              },
              "leading_diamond": "\uE0c5",
              "trailing_diamond": "\uE0B0"
            },
            {
              "type": "session",
              "style": "powerline",
              "foreground": "#ffffff",
              "background": "#3A86FF",
              "powerline_symbol": "\uE0B0"
            },
            {
              "type": "battery",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#193549",
              "background": "#a2beef",
              "properties": {
                "battery_icon": "\f583",
                "color_background": true,
                "charged_color": "#0476d0",
                "charging_color": "#00D100",
                "discharging_color": "#FFCD58",
                "postfix": "\uF295 \uf583 "
              }
            },
            {
              "type": "executiontime",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#ffffff",
              "background": "#8800dd",
              "properties": {
                "always_enabled": true,
                "style": "austin",
                "prefix": " <#fefefe>\ufbab</> "
              }
            },
            {
              "type": "path",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#ffffff",
              "background": "#ff479c",
              "properties": {
                "prefix": " \uE5FF ",
                "style": "full"
              }
            },
            {
              "type": "time",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#193549",
              "background": "#4caf50",
              "properties": {
                "time_format": "2006-01-02 15:04:05",
                "prefix": "\uFA1F"
              }
            },
            {
              "type": "git",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#193549",
              "background": "#fffb38",
              "properties": {
                "display_stash_count": true,
                "display_upstream_icon": true
              }
            },
            {
              "type": "node",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#ffffff",
              "background": "#6CA35E",
              "properties": {
                "prefix": " \uE718 "
              }
            },
            {
              "type": "root",
              "style": "powerline",
              "powerline_symbol": "\uE0B0",
              "foreground": "#193549",
              "background": "#ffff66"
            },
            {
              "type": "exit",
              "style": "diamond",
              "foreground": "#ffffff",
              "background": "#2e9599",
              "leading_diamond": "<transparent,#2e9599>\uE0B0</>",
              "trailing_diamond": "\uE0B4",
              "properties": {
                "display_exit_code": true,
                "always_enabled": true,
                "error_color": "#f1184c",
                "color_background": true,
                "prefix": " \ue3bf "
              }
            }
          ]
        },
        {
          "type": "prompt",
          "alignment": "left",
          "newline": true,
          "segments": [
            {
              "type": "shell",
              "style": "diamond",
              "foreground": "#ffffff",
              "background": "#0077c2",
              "leading_diamond": "\uE0c5",
              "trailing_diamond": "\uE0B0",
              "properties": {
                "mapped_shell_names": {
                  "pwsh": "PS"
                },
                "prefix": " \uf489 ",
                "postfix": " \uE0B1"
              }
            }
          ]
        }
      ],
      "final_space": true
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    {
      "blocks": [
          {
              "type": "prompt",
              "alignment": "left",
              "segments": [
                  {
                      "type": "os",
                      "style": "plain",
                      "properties": {
                          "macos": "mac"
                      },
                      "foreground": "#26C6DA"
                  },
                  {
                      "type": "session",
                      "style": "plain",
                      "foreground": "#3A86FF"
                  },
                  {
                      "type": "executiontime",
                      "style": "plain",
                      "foreground": "#8800dd",
                      "properties": {
                          "always_enabled": true,
                          "style": "austin"
                      }
                  },
                  {
                      "type": "path",
                      "style": "plain",
                      "foreground": "#ff479c",
                      "properties": {
                          "style": "full",
                          "prefix": " \uf07b ",
                          "folder_separator_icon": "/"
                      }
                  },
                  {
                      "type": "time",
                      "style": "plain",
                      "foreground": "#4caf50",
                      "properties": {
                          "time_format": "2006-01-02 15:04:05"
                      }
                  },
                  {
                      "type": "git",
                      "style": "plain",
                      "powerline_symbol": "\uE0B0",
                      "foreground": "#14A5AE",
                      "properties": {
                          "display_stash_count": true,
                          "display_upstream_icon": true,
                          "prefix": ""
                      }
                  },
                  {
                      "type": "node",
                      "style": "plain",
                      "powerline_symbol": "\uE0B0",
                      "foreground": "#6CA35E",
                      "properties": {
                          "prefix": " \uE718 "
                      }
                  },
                  {
                      "type": "root",
                      "style": "plain",
                      "foreground": "#ffff66"
                  },
                  {
                      "type": "exit",
                      "style": "plain",
                      "foreground": "#2e9599",
                      "properties": {
                          "display_exit_code": true,
                          "always_enabled": true,
                          "error_color": "#f1184c",
                          "prefix": " \ue3bf "
                      }
                  }
              ]
          },
          {
              "type": "prompt",
              "alignment": "left",
              "newline": true,
              "segments": [
                  {
                      "type": "shell",
                      "style": "diamond",
                      "foreground": "#0077c2",
                      "properties": {
                          "mapped_shell_names": {
                              "pwsh": "PS"
                          },
                          "prefix": " \uf489 ",
                          "postfix": " \uE0B1 "
                      }
                  }
              ]
          }
      ]
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    // Make sure to add code blocks to your code group

    然后将$.omp.json文件放入到~文件夹(即当前用户目录)里,编辑$PROFILE使其永久生效。

    官方配置文件教程:点击链接 (opens new window)

    cd ~
    # 查看当前路径
    get-location
    # 编辑$.omp.json
    code $.omp.json
    # 编辑$PROFILE文件
    code $PROFILE
    # 添加下面命令
    Set-PoshPrompt -Theme ~/$.omp.json
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    - img: https://cdn.jsdelivr.net/gh/Awrtiger/mirrorfile/oh-my-posh/tiger.png
      link: https://ool.cool/blog/006/#%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%BB%E9%A2%98
      name: 最终效果 tiger.omp.json
    
    1
    2
    3
    - img: https://cdn.jsdelivr.net/gh/Awrtiger/mirrorfile/oh-my-posh/tiger-light.png
      link: https://ool.cool/blog/006/#%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%BB%E9%A2%98
      name: 最终效果 tiger-light.omp.json
    
    1
    2
    3

    # PowerShell特别处理

    1. powershell.exe -nologo 即不会提示版权等条幅。
    2. 编辑$PROFILE文件,添加个性图案,图案生成 (opens new window),利用Write-Output " "