Windows系统设置PowerShell代理及别名

Win系统PowerShell设置代理及使用别名

创建永久的别名

在PowerShell中直接使用Set-Alias或New-Alias命令创建的别名在关闭此Session后即会失效,防止此现象的方法是将此命令写入Windows PowerShell profile文件。

PS C:\> $profile

一般该文件在没有创建前是不存在的,使用以下命令为当前用户创建profile命令并返回文件地址:

PS C:\> New-Item -Type file -Force $profile

一般创建的位置在~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
打开文件,键入文件内容为Get-ChildItem -Name创建别名ls:

1
2
3
4
5
function scr {
scrcpy -S
}
# Remove-Item alias:\ls
# Set-Alias ls scr

这里首先为Get-ChildItem -Name创建了方法getFileName作为中介,然后为该方法赋予别名ls,但是因为ls是Windows PowerShell中的默认别名,因此必须先删除再创建,所以先使用Remove-Item再使用Set-Alias或New-Alias。
以后每次在打开PowerShell会话框的时候其会先读取$profile文件中的内容。

使用代理

1
2
3
4
5
6
7
8
function proxy {
$env:HTTP_PROXY="http://127.0.0.1:7890"
}

function proxys {
$env:HTTPS_PROXY="http://127.0.0.1:7890"
}