在 Windows Terminal 里使用, 有些踩坑经验[^1]
Install powershell in D driver
如果你需要将PowerShell 7.x(注意:win的安装包在show more里)与其他版本并排运行,请使用ZIP安装方法将其他版本安装到不同的文件夹。
1 2 3 4 5
| PS C:\Users\94364> $psversiontable
Name Value ---- ----- PSVersion 5.1.19041.4780
|
Bashrc-like 配置文件
存放路径与文件
1 2 3 4 5 6 7
| E:/PowerShell via v14.17.3 via 🐍 v3.9.7 ❯ echo $PSHOME E:\commonSoftware\PowerShell7
E:/PowerShell via v14.17.3 via 🐍 v3.9.7 ❯ echo $PROFILE D:\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
|
常见的设置
title1 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
| $env:DISPLAY="localhost:0.0" Import-Module PSColor Import-Module DirColors Import-Module posh-git # 引入 posh-git Import-Module oh-my-posh # 引入 oh-my-posh
#Set-Theme Paradox 设置主题为 Paradox
Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录 Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全 Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销 Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录 Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录 Set-PSReadLineOption -Colors @{ InlinePrediction = '#DF6C75'}
Invoke-Expression (&starship init powershell) function Pro {notepad $PROFILE} function Get-CmdletAlias ($cmdletname) { Get-Alias | Where-Object -FilterScript {$_.Definition -like "$cmdletname"} | Format-Table -Property Definition, Name -AutoSize } $env:TEMP="E:\Temp" $env:TMP="E:\Temp" $env:GIT_CURL_VERBOSE = 1 $env:GIT_TRACE = 1 Write-Host "Hi Mike, welcome back!" $HOMEDRIVE = "D:" $HOMEPATH = "\PowerShell" Remove-Variable -Force HOME #Set-Variable HOME "E:\PowerShell" # Set and force overwrite of the $HOME variable Set-Variable HOME "$HOMEDRIVE$HOMEPATH" -Force
# Set the "~" shortcut value for the FileSystem provider (get-psprovider 'FileSystem').Home = $HOMEDRIVE + $HOMEPATH Set-location E:\PowerShell
Function Format-FileSize() { Param ([int64]$size) If ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)} ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)} ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)} ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)} ElseIf ($size -gt 0) {[string]::Format("{0:0.00} B", $size)} Else {""} }
|
1 2 3 4 5 6 7
| . : 无法加载文件 C:\Users\94364\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁止运行脚 本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 3 + . 'C:\Users\94364\Documents\WindowsPowerShell\Microsoft.PowerShell_pr ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [],PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
|
权限问题
环境变量
- 查看:运行
dir env:
显示
- 修改:
$env:TMP="D:\Temp"
Proxy
1 2
| $env:http_proxy = "http://127.0.0.1:7890" $env:https_proxy = "http://127.0.0.1:7890"
|
PATH
echo $env:PATH| tr ";" "\n"| sort
ssh
Win32-OpenSSH they go to %USERPROFILE%\.ssh.
That typically is: C:\Users\username\.ssh
1 2 3
| ssh -v xxx@xxx ls ~/.ssh echo $HOME
|
常用命令
Kill Process by CLI
title1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $keyword = Read-Host "请输入要匹配的关键字"
$processes = Get-Process | Where-Object { $_.ProcessName -like "*$keyword*" } $processes
if ($processes.Count -eq 0) { Write-Host "没有找到匹配的进程" exit }
$response = Read-Host "是否结束这些进程? (y/n)"
if ($response -eq 'y') { $processes | Stop-Process Write-Host "进程已结束" } else { Write-Host "操作已取消" }
|
Linux vs Windows Commands
参考文献
[^1]: notion tips
[^2]: Linux vs Windows Commands