powershell自动提示、快速跳转、代理设置

powershell

powershell配置文件位置

$profile.CurrentUserAllHosts该指令返回的配置文件路径在普通模式与管理员模式下均生效

powershell 自动提示和快速跳转

install-module PSReadLine -requiredVersion 2.1.0 安装2.1.0版(或以上) psreadline
在配置文件中写入Set-PSReadLineOption -PredictionSource History,开启自动提示
install-module ZLocation,安装后使用z快速跳转

代理

powershell 代理

设置当前窗口代理 :set-proxy

设置当前窗口代理 + 系统代理:set-proxy -ApplyToSystem

取消当前窗口代理:clear-proxy

取消当前窗口代理 + 系统代理:clear-proxy -ApplyToSystem

set-proxy和SetProxy,clear-proxy和ClearProxy可以互相替换
下面内容写入powershell配置文件

# Set-Proxy command
if ($env:HTTP_PROXY -ne $null){
    Write-Output "Proxy Enabled as $env:HTTP_PROXY";
}

Function SetProxy() {
    Param(
        # 改成自己的代理地址
        $Addr = 'http://127.0.0.1:19810',
        [switch]$ApplyToSystem
    )

    $env:HTTP_PROXY = $Addr;
    $env:HTTPS_PROXY = $Addr;
    $env:http_proxy = $Addr;
    $env:https_proxy = $Addr;

    [Net.WebRequest]::DefaultWebProxy = New-Object Net.WebProxy $Addr;
    if ($ApplyToSystem) {
        $matchedResult = ValidHttpProxyFormat $Addr;
        # Matched result: [URL Without Protocol][Input String]
        if (-not ($matchedResult -eq $null)) {
            SetSystemProxy $matchedResult.1;
        }
    }
    Write-Output "Successful set proxy as $Addr";
}
Function ClearProxy() {
    Param(
        $Addr = $null,
        [switch]$ApplyToSystem
    )

    $env:HTTP_PROXY = $Addr;
    $env:HTTPS_PROXY = $Addr;
    $env:http_proxy = $Addr;
    $env:https_proxy = $Addr;

    [Net.WebRequest]::DefaultWebProxy = New-Object Net.WebProxy;
    if ($ApplyToSystem) { SetSystemProxy $null; }
    Write-Output "Successful unset all proxy variable";

}
Function SetSystemProxy($Addr = $null) {
    Write-Output $Addr
    $proxyReg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings";

    if ($Addr -eq $null) {
        Set-ItemProperty -Path $proxyReg -Name ProxyEnable -Value 0;
        return;
    }
    Set-ItemProperty -Path $proxyReg -Name ProxyServer -Value $Addr;
    Set-ItemProperty -Path $proxyReg -Name ProxyEnable -Value 1;
}
Function ValidHttpProxyFormat ($Addr) {
    $regex = "(?:https?:\/\/)(\w+(?:.\w+)*(?::\d+)?)";
    $result = $Addr -match $regex;
    if ($result -eq $false) {
        throw [System.ArgumentException]"The input $Addr is not a valid HTTP proxy URI.";
    }

    return $Matches;
}
Set-Alias set-proxy SetProxy
Set-Alias clear-proxy ClearProxy

cmd 代理

set http_proxy=http://127.0.0.1:19810
set https_proxy=http://127.0.0.1:19810

你可能感兴趣的:(powershell自动提示、快速跳转、代理设置)