PowerShell 使用经验积累
本文主要介绍了 PowerShell 的使用经验,包括常用命令、终端美化等。
PowerShell Documentation - PowerShell | Microsoft Learn
PowerShell 101 - PowerShell | Microsoft Learn
PowerShell 常用命令
使用 Get-Alias
命令就可以查看所有的命令别名。
about Aliases - PowerShell | Microsoft Learn
Microsoft.PowerShell.Core Module - PowerShell | Microsoft Learn
Alias | Command | Function |
---|---|---|
clc | Clear-Content | |
clhy | Clear-History | 从命令历史记录文件中删除命令历史 |
clear | Clear-Host | 清除当前 Host 的命令历史展示 |
copy/cp/cpi | Copy-Item | 复制文件 |
gal | Get-Alias | 查看命令别名 |
gci/ls/dir | Get-ChildItem | 获取当前文件夹下文件及子文件夹 |
gcm | Get-Command | 查询命令信息 |
gl/pwd | Get-Location | 获取当前文件夹 |
ghy/h/history | Get-History | 历史命令 |
man/help | Get-Help | 帮助 |
mi/mv/move | Move-Item | 移动文件 |
ni/md/mkdir | New-Item | 创建文件夹 |
rm/rmdir | Remove-Item | 删除文件 |
ren/rni | Rename-Item | 重命名文件 |
sl/cd/chdir | Set-Location | 设置当前工作目录 |
where/? | Where-Object | 根据对象的属性值从集合中选择对象 |
PowerShell 和 Windows PowerShell 的区别
Install PowerShell on Windows, Linux, and macOS - PowerShell | Microsoft Learn
Differences between Windows PowerShell 5.1 and PowerShell 7.x - PowerShell | Microsoft Learn
PowerShell differences on non-Windows platforms - PowerShell | Microsoft Learn
由于 PowerShell 的演进,它从 Windows 专属的工具变成了跨平台的工具,适用于 Windows 系统的 PowerShell 称为 Windows PowerShell。Linux 和 macOS 系统上的 PowerShell 基于 .NET core 开发,而 .NET core 是完全版的 .NET Framework 的子集。由于使用框架的不同,导致有一些在 Windows 系统上可以运行脚本无法在其他的系统上运行。
Windows 用户层面需要注意到的区别有:
- PowerShell 的可执行文件为 pwsh. Exe, Windows PowerShell 的可执行文件是 powershell. Exe.
- 安装文件文件夹的区别
1 | C:\Program Files\PowerShell\7\pwsh.exe # PowerShell |
- 配置文件文件夹的区别
1 |
|
PowerShell Gallery
PowerShellGet and the PowerShell Gallery - PowerShell | Microsoft Learn
PowerShell 终端美化
Windows Terminal Custom Prompt Setup
- 下载安装
1 | winget install JanDeDobbeleer.OhMyPosh -s winget # 安装 Oh My Posh |
- 配置:需要安装 Nerd Fonds 系列字体
- Oh My Posh 安装好之后会添加环境变量
POSH_THEMES_PATH
,主题就在此文件夹下,可以在 PowerShell 的配置文件中添加如下内容以启用某一主题:
1 | nvim $profile # 使用 Nvim 编辑 Profile 文件 |
- 在
$home
目录显示 Python 虚拟环境,在主题文件里增加如下内容:
1 | "properties": { |
使用经验
更改 Power Shell 的执行策略
在计算机上启动 Windows PowerShell 时,执行策略很可能是 Restricted(默认设置)。
PowerShell 提示”无法加载文件,因为在此系统上禁止运行脚本”的解决方法
设定 PowerShell 启动位置
Set the position of Powershell window
自定义 Powershell Scripts
How to Write and Run Scripts in the Windows PowerShell ISE - PowerShell
PowerShell 使用经验积累