PowerShell美化

先上最终效果图

image-20220419135827677

image-20220419141105906

1 安装 windows terminal

有两种方式,一种是微软商店,搜索安装即可。

另一种是github仓库下载安装,可以安装预览版,也很稳定,就是不用再用微软商店更新了。

microsoft/terminal: The new Windows Terminal and the original Windows console host, all in the same place! (github.com)

2 安装字体

因为后面安装的主题可能很多字符显示不了,所以需要安装的新字体。

推荐的字体的名字是:FiraCode NF

先下载字体

Releases · ryanoasis/nerd-fonts (github.com)

image-20220419134021286

然后安装压缩包里的字体,可以全部安装也可以安装某一个,之后就可以在设置里修改字体了,比如windows terminalvscode终端字体

3 安装 powershell 7

Releases · PowerShell/PowerShell (github.com)

4 安装Powershell插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 设置权限
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

# 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
Install-Module -Name PSReadLine -Scope CurrentUser

# 2. 安装 posh-git 包,让你的 git 更好用
Install-Module posh-git -Scope CurrentUser

# 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
Install-Module oh-my-posh -Scope CurrentUser

# 3. 安装 PSColor 包
Install-Module PSColor -Scope CurrentUser

5 terminal 配置

有时terminal不能自动识别已经安装了新的powershell,需要手动添加,设置启动位置和图标

image-20220419140109816

更改字体大小,字体、配色方案等等

image-20220419140218149

之后可在json文件中修改其他高级设置,如背景,透明度等等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"profiles": 
{
"defaults":
{
"backgroundImage": "C:\\Users\\xxx\\Pictures\\壁纸\\xxx.png",
"backgroundImageOpacity": 0.22,
"backgroundImageStretchMode": "uniformToFill",
"colorScheme": "Flat UI Palette v1 Modified",
"cursorColor": "#FFFFFF",
"cursorShape": "bar",
"font":
{
"face": "FiraCode NF",
"size": 12
},
"opacity": 80,
"startingDirectory": "C:\\Users\\xxx\\Desktop",
"useAcrylic": false
},
"list":
...

提供一个配色方案:Flat UI Palette v1 Modified,放到schemes里面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"background": "#1C2024",
"black": "#000000",
"blue": "#2980B9",
"brightBlack": "#52677C",
"brightBlue": "#3498DB",
"brightCyan": "#1ABC9C",
"brightGreen": "#2ECC71",
"brightPurple": "#9B59B6",
"brightRed": "#E67E22",
"brightWhite": "#ECF0F1",
"brightYellow": "#F1C40F",
"cursorColor": "#FFFFFF",
"cyan": "#16A085",
"foreground": "#ECF0F1",
"green": "#27AE60",
"name": "Flat UI Palette v1 Modified",
"purple": "#8E44AD",
"red": "#E74C3C",
"selectionBackground": "#FFFFFF",
"white": "#FFFFFF",
"yellow": "#F1C40F"
},

6 ps配置启动参数

在用户文档中新建一个 ps1文件,位置:

C:\Users\xxx\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

这里面主要修改的主题和用户的一些小功能,如开启代理

通过Get-PoshThemes或者Get-Theme查看可选主题以及样式

image-20220419141113769

详细配置

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
#------------------------------- Import Modules BEGIN -------------------------------
# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
Import-Module oh-my-posh

# 引入 ps-read-line
Import-Module PSReadLine

# 设置 PowerShell 主题
# Set-PoshPrompt ys
Set-PoshPrompt velvet

Import-Module PSColor
#------------------------------- Import Modules END -------------------------------



#------------------------------- Set Hot-keys BEGIN -------------------------------
# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History

# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd

# 设置 Tab 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#------------------------------- Set Hot-keys END -------------------------------


#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "D:\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

function set_proxy_variable {
$env:http_proxy = "127.0.0.1:1080"
$env:https_proxy = "127.0.0.1:1080"
}

function unset_proxy_variable {
echo "close proxy"
$env:http_proxy = ""
$env:https_proxy = ""
}

function print_proxy_variable {
echo $env:http_proxy
echo $env:https_proxy
}

New-Alias -Name spp -Value set_proxy_variable
New-Alias -Name upp -Value unset_proxy_variable
New-Alias -Name ppp -Value print_proxy_variable

New-Alias -Name ll -Value ls

有时候第一部分需要改成下面:oh-my-posh不支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#------------------------------- Import Modules BEGIN -----------------------------
# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
# Import-Module oh-my-posh

# 引入 ps-read-line
Import-Module PSReadLine

# 设置 PowerShell 主题
# Set-PoshPrompt ys
# Set-PoshPrompt velvet
oh-my-posh init pwsh --config C:\\Users\\299\\AppData\\Local\\Programs\\oh-my-posh\\themes\\velvet.omp.json | Invoke-Expression

Import-Module PSColor
#------------------------------- Import Modules END -----------------------------

7 右键菜单启动

有时不用这一步,因为 windows terminal可能自带了

安装

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#Requires -RunAsAdministrator
#Requires -Version 6

[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet('Default', 'Flat', 'Mini')]
[string] $Layout = 'Mini',
[Parameter()]
[switch] $PreRelease
)





# 生成帮助脚本
function Generate-HelperScript(
# The cache folder
[Parameter(Mandatory=$true)]
[string]$cache) {
$content =
"Set shell = WScript.CreateObject(`"Shell.Application`")
executable = WSCript.Arguments(0)
folder = WScript.Arguments(1)
If Wscript.Arguments.Count > 2 Then
profile = WScript.Arguments(2)
' 0 at the end means to run this command silently
shell.ShellExecute `"powershell`", `"Start-Process \`"`"`" & executable & `"\`"`" -ArgumentList \`"`"-p \`"`"\`"`"`" & profile & `"\`"`"\`"`" -d \`"`"\`"`"`" & folder & `"\`"`"\`"`" \`"`" `", `"`", `"runas`", 0
Else
' 0 at the end means to run this command silently
shell.ShellExecute `"powershell`", `"Start-Process \`"`"`" & executable & `"\`"`" -ArgumentList \`"`"-d \`"`"\`"`"`" & folder & `"\`"`"\`"`" \`"`" `", `"`", `"runas`", 0
End If
"
Set-Content -Path "$cache/helper.vbs" -Value $content
}





# 获取 icon
# 源地址:https://github.com/Duffney/PowerShell/blob/master/FileSystems/Get-Icon.ps1
Function Get-Icon {

[CmdletBinding()]

Param (
[Parameter(Mandatory=$True, Position=1, HelpMessage="Enter the location of the .EXE file")]
[string]$File,

# If provided, will output the icon to a location
[Parameter(Position=1, ValueFromPipelineByPropertyName=$true)]
[string]$OutputFile
)

[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null

[System.Drawing.Icon]::ExtractAssociatedIcon($File).ToBitmap().Save($OutputFile)
}





# 转换为 icon
# https://gist.github.com/darkfall/1656050
function ConvertTo-Icon {
<#
.Synopsis
Converts image to icons
.Description
Converts an image to an icon
.Example
ConvertTo-Icon -File .\Logo.png -OutputFile .\Favicon.ico
#>
[CmdletBinding()]
param(
# The file
[Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
[Alias('Fullname')]
[string]$File,

# If provided, will output the icon to a location
[Parameter(Position=1, ValueFromPipelineByPropertyName=$true)]
[string]$OutputFile
)

begin {
Add-Type -AssemblyName System.Drawing
}

process {
#region Load Icon
$resolvedFile = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($file)
if (-not $resolvedFile) { return }
$inputBitmap = [Drawing.Image]::FromFile($resolvedFile)
$width = $inputBitmap.Width
$height = $inputBitmap.Height
$size = New-Object Drawing.Size $width, $height
$newBitmap = New-Object Drawing.Bitmap $inputBitmap, $size
#endregion Load Icon

#region Save Icon
$memoryStream = New-Object System.IO.MemoryStream
$newBitmap.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Png)

$resolvedOutputFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($outputFile)
$output = [IO.File]::Create("$resolvedOutputFile")

$iconWriter = New-Object System.IO.BinaryWriter($output)
# 0-1 reserved, 0
$iconWriter.Write([byte]0)
$iconWriter.Write([byte]0)

# 2-3 image type, 1 = icon, 2 = cursor
$iconWriter.Write([short]1);

# 4-5 number of images
$iconWriter.Write([short]1);

# image entry 1
# 0 image width
$iconWriter.Write([byte]$width);
# 1 image height
$iconWriter.Write([byte]$height);

# 2 number of colors
$iconWriter.Write([byte]0);

# 3 reserved
$iconWriter.Write([byte]0);

# 4-5 color planes
$iconWriter.Write([short]0);

# 6-7 bits per pixel
$iconWriter.Write([short]32);

# 8-11 size of image data
$iconWriter.Write([int]$memoryStream.Length);

# 12-15 offset of image data
$iconWriter.Write([int](6 + 16));

# write image data
# png data must contain the whole png data file
$iconWriter.Write($memoryStream.ToArray());

$iconWriter.Flush();
$output.Close()
#endregion Save Icon

#region Cleanup
$memoryStream.Dispose()
$newBitmap.Dispose()
$inputBitmap.Dispose()
#endregion Cleanup
}
}





# 获取 Program Files 文件夹
function GetProgramFilesFolder(
[Parameter(Mandatory=$true)]
[bool]$includePreview) {
$versionFolders = (Get-ChildItem "$Env:ProgramFiles\WindowsApps" | Where-Object {
if ($includePreview) {
$_.Name -like "Microsoft.WindowsTerminal_*__*" -or
$_.Name -like "Microsoft.WindowsTerminalPreview_*__*"
} else {
$_.Name -like "Microsoft.WindowsTerminal_*__*"
}
})
$foundVersion = $null
$result = $null
foreach ($versionFolder in $versionFolders) {
if ($versionFolder.Name -match "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") {
$version = [version]$Matches.0
Write-Host "Found Windows Terminal version $version."
if ($null -eq $foundVersion -or $version -gt $foundVersion) {
$foundVersion = $version
$result = $versionFolder.FullName
}
} else {
Write-Warning "Found Windows Terminal unsupported version in $versionFolder."
}
}

if ($foundVersion -lt [version]"0.11") {
Write-Warning "The latest version found is less than 0.11, which is not tested. The install script might fail in certain way."
}

if ($null -eq $result) {
Write-Error "Failed to find Windows Terminal actual folder. The install script might fail in certain way."
}

return $result
}





# 获取 Windows Terminal 的 icon
function GetWindowsTerminalIcon(
[Parameter(Mandatory=$true)]
[string]$folder,
[Parameter(Mandatory=$true)]
[string]$localCache) {
$icon = "$localCache\wt.ico"
$actual = $folder + "\WindowsTerminal.exe"
if (Test-Path $actual) {
# use app icon directly.
Write-Host "Found actual executable $actual."
$temp = "$localCache\wt.png"
Get-Icon -File $actual -OutputFile $temp
ConvertTo-Icon -File $temp -OutputFile $icon
} else {
# download from GitHub
Write-Warning "Didn't find actual executable $actual so download icon from GitHub."
Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/terminal/master/res/terminal.ico" -OutFile $icon
}

return $icon
}





# 获取 Windows Terminal 的配置文件
function GetActiveProfiles(
[Parameter(Mandatory=$true)]
[bool]$isPreview) {
if ($isPreview) {
$file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json"
} else {
$file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
}
if (-not (Test-Path $file)) {
Write-Error "Couldn't find profiles. Please run Windows Terminal at least once after installing it. Exit."
exit 1
}

$settings = Get-Content $file | Out-String | ConvertFrom-Json
if ($settings.profiles.PSObject.Properties.name -match "list") {
$list = $settings.profiles.list
} else {
$list = $settings.profiles
}

return $list | Where-Object { -not $_.hidden} | Where-Object { ($null -eq $_.source) -or -not ($settings.disabledProfileSources -contains $_.source) }
}





function GetProfileIcon (
[Parameter(Mandatory=$true)]
$profile,
[Parameter(Mandatory=$true)]
[string]$folder,
[Parameter(Mandatory=$true)]
[string]$localCache,
[Parameter(Mandatory=$true)]
[string]$defaultIcon,
[Parameter(Mandatory=$true)]
[bool]$isPreview) {
$guid = $profile.guid
$name = $profile.name
$result = $null
$profilePng = $null
$icon = $profile.icon
if ($null -ne $icon) {
if (Test-Path $icon) {
# use user setting
$profilePng = $icon
} elseif ($profile.icon -like "ms-appdata:///Roaming/*") {
#resolve roaming cache
if ($isPreview) {
$profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
} else {
$profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
}
} elseif ($profile.icon -like "ms-appdata:///Local/*") {
#resolve local cache
if ($isPreview) {
$profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\"
} else {
$profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\"
}
} elseif ($profile.icon -like "ms-appx:///*") {
# resolve app cache
$profilePng = $icon -replace "ms-appx://", $folder -replace "/", "\"
} elseif ($profile.icon -like "*%*") {
$profilePng = [System.Environment]::ExpandEnvironmentVariables($icon)
} else {
Write-Host "Invalid profile icon found $icon. Please report an issue at https://github.com/lextm/windowsterminal-shell/issues ."
}
}

if (($null -eq $profilePng) -or -not (Test-Path $profilePng)) {
# fallback to profile PNG
$profilePng = "$folder\ProfileIcons\$guid.scale-200.png"
if (-not (Test-Path($profilePng))) {
if ($profile.source -eq "Windows.Terminal.Wsl") {
$profilePng = "$folder\ProfileIcons\{9acb9455-ca41-5af7-950f-6bca1bc9722f}.scale-200.png"
}
}
}

if (Test-Path $profilePng) {
if ($profilePng -like "*.png") {
# found PNG, convert to ICO
$result = "$localCache\$guid.ico"
ConvertTo-Icon -File $profilePng -OutputFile $result
} elseif ($profilePng -like "*.ico") {
$result = $profilePng
} else {
Write-Warning "Icon format is not supported by this script $profilePng. Please use PNG or ICO format."
}
} else {
Write-Warning "Didn't find icon for profile $name."
}

if ($null -eq $result) {
# final fallback
$result = $defaultIcon
}

return $result
}





function CreateMenuItem(
[Parameter(Mandatory=$true)]
[string]$rootKey,
[Parameter(Mandatory=$true)]
[string]$name,
[Parameter(Mandatory=$true)]
[string]$icon,
[Parameter(Mandatory=$true)]
[string]$command,
[Parameter(Mandatory=$true)]
[bool]$elevated) {
New-Item -Path $rootKey -Force | Out-Null
New-ItemProperty -Path $rootKey -Name 'MUIVerb' -PropertyType String -Value $name | Out-Null
New-ItemProperty -Path $rootKey -Name 'Icon' -PropertyType String -Value $icon | Out-Null
if ($elevated) {
New-ItemProperty -Path $rootKey -Name 'HasLUAShield' -PropertyType String -Value '' | Out-Null
}

New-Item -Path "$rootKey\command" -Force | Out-Null
New-ItemProperty -Path "$rootKey\command" -Name '(Default)' -PropertyType String -Value $command | Out-Null
}





function CreateProfileMenuItems(
[Parameter(Mandatory=$true)]
$profile,
[Parameter(Mandatory=$true)]
[string]$executable,
[Parameter(Mandatory=$true)]
[string]$folder,
[Parameter(Mandatory=$true)]
[string]$localCache,
[Parameter(Mandatory=$true)]
[string]$icon,
[Parameter(Mandatory=$true)]
[string]$layout,
[Parameter(Mandatory=$true)]
[bool]$isPreview) {
$guid = $profile.guid
$name = $profile.name
$command = """$executable"" -p ""$name"" -d ""%V."""
$elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V."" ""$name"""
$profileIcon = GetProfileIcon $profile $folder $localCache $icon $isPreview

if ($layout -eq "Default") {
$rootKey = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid"
$rootKeyElevated = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid"
CreateMenuItem $rootKey $name $profileIcon $command $false
CreateMenuItem $rootKeyElevated $name $profileIcon $elevated $true
} elseif ($layout -eq "Flat") {
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "在终端中以管理员身份打开" $profileIcon $elevated $true
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "在终端中以管理员身份打开" $profileIcon $elevated $true
}
}





function CreateMenuItems(
[Parameter(Mandatory=$true)]
[string]$executable,
[Parameter(Mandatory=$true)]
[string]$layout,
[Parameter(Mandatory=$true)]
[bool]$includePreview) {
$folder = GetProgramFilesFolder $includePreview
$localCache = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\Cache"

if (-not (Test-Path $localCache)) {
New-Item $localCache -ItemType Directory | Out-Null
}

Generate-HelperScript $localCache
$icon = GetWindowsTerminalIcon $folder $localCache

if ($layout -eq "Default") {
# defaut layout creates two menus
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal Here' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal Here' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell' -Force | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value '在终端中以管理员身份打开' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value '在终端中以管理员身份打开' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell' -Force | Out-Null
} elseif ($layout -eq "Mini") {
$elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V."""
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini_Admin" "在终端中以管理员身份打开" $icon $elevated $true
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini_Admin" "在终端中以管理员身份打开" $icon $elevated $true
return
}

$isPreview = $folder -like "*WindowsTerminalPreview*"
$profiles = GetActiveProfiles $isPreview
foreach ($profile in $profiles) {
CreateProfileMenuItems $profile $executable $folder $localCache $icon $layout $isPreview
}
}





# Based on @nerdio01's version in https://github.com/microsoft/terminal/issues/1060
if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and
-not (Test-Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal")) {
Write-Error "Please execute uninstall.old.ps1 to remove previous installation."
exit 1
}

if ($PSVersionTable.PSVersion.Major -lt 6) {
Write-Error "Must be executed in PowerShell 6 and above. Learn how to install it from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7 . Exit."
exit 1
}

$executable = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\wt.exe"
if (-not (Test-Path $executable)) {
Write-Error "Windows Terminal not detected. Learn how to install it from https://github.com/microsoft/terminal (via Microsoft Store is recommended). Exit."
exit 1
}





# 实际执行语句
Write-Host "布局风格:$Layout"
CreateMenuItems $executable $Layout $PreRelease
Write-Host "Windows Terminal 启动选项已添加到资源管理器右键菜单"
Write-Host ""
Write-Host "P.S. 卸载请使用 .\uninstall.ps1 $Layout"

卸载

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
#Requires -Version 6

[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet('Default', 'Flat', 'Mini')]
[string] $Layout = 'Mini',
[Parameter()]
[switch] $PreRelease
)

# Based on @nerdio01's version in https://github.com/microsoft/terminal/issues/1060

if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and
-not (Test-Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal")) {
Write-Error "Please execute uninstall.old.ps1 to remove previous installation."
exit 1
}

$localCache = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\Cache"
if (Test-Path $localCache) {
Remove-Item $localCache -Recurse
}

Write-Host "布局风格:$Layout"

if ($layout -eq "Default") {
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell' -Recurse -ErrorAction Ignore | Out-Null
} elseif ($layout -eq "Flat") {
$rootKey = 'HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell'
foreach ($key in Get-ChildItem -Path "Registry::$rootKey") {
if (($key.Name -like "$rootKey\MenuTerminal_*") -or ($key.Name -like "$rootKey\MenuTerminalAdmin_*")) {
Remove-Item "Registry::$key" -Recurse -ErrorAction Ignore | Out-Null
}
}

$rootKey = 'HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell'
foreach ($key in Get-ChildItem -Path "Registry::$rootKey") {
if (($key.Name -like "$rootKey\MenuTerminal_*") -or ($key.Name -like "$rootKey\MenuTerminalAdmin_*")) {
Remove-Item "Registry::$key" -Recurse -ErrorAction Ignore | Out-Null
}
}
} elseif ($layout -eq "Mini") {
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini_Admin' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini' -Recurse -ErrorAction Ignore | Out-Null
Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini_Admin' -Recurse -ErrorAction Ignore | Out-Null
}

Write-Host "Windows Terminal 启动项已经从资源管理器右键菜单移除。"