0%

PowerShell 创建 Profile

查看 Profile 路径

1
$Profile

创建一个 Profile

1
2
New-Item -Path $Profile -Type File
# -Force 是强制创建,加上后即使已存在 Profile,也创建并覆盖原来的 Profile

使用记事本编辑 Profile

1
2
3
notepad $Profile
# Profile 里面,可以输入任何你在ps中输入的命令、函数
# (如:个性化自己的ps界面、自己常用的函数等)

方法案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 测试 Baidu 连接,设置调用别名 trybaidu
function Test-BaiduConnection
{
Test-Connection www.baidu.com
}
Set-Alias trybaidu Test-BaiduConnection

# 编辑 hosts 文件,设置调用别名 eh
function Edit-Host
{
Start-Process notepad -Verb runas -ArgumentList "$env:windir\System32\drivers\etc\hosts" -wait
ipconfig /flushdns | Out-Null
}
Set-Alias eh Edit-Host

如遇到无法加载 ps1 文件

1
2
3
4
5
6
7
# 以管理员身份打开 PowerShell

# 查看系统执行策略状态
get-ExecutionPolicy

# 修改执行策略状态
set-executionpolicy remotesigned