配置默认powershell启动profile

1. 首先设置允许加载自定义脚本

默认powershell是不能加载自定义脚本,但是你可以修改系统配置,让powershell能加载系统配置

开启一个Admin powershell,然后运行

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 

 

2. 设置启动powershell时的加载脚本

参考文档:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-6


参见文档,你可以配置profile文件,让powershell启动的时候,就可以拥有一些定制化功能,


你可以在开启的powershell命令行中,输入命令,打开profile文件

code $PROFILE.AllUsersAllHosts

然后在打开的编辑其中,输入代码

例如

Write-Host "`n fly -> Enable proxy `n"
Write-Host "`n land -> Disable proxy`n"
Set-Alias fly Set-Proxy-XXX
Set-Alias land Clear-Proxy-XXX
function Display-Proxy
{
    if (Test-Path Env:HTTP_PROXY) {
        Get-ChildItem Env:HTTP_PROXY
    } else {
        Write-Host "`n Env:HTTP_PROXY not found`n"
    }
    if (Test-Path Env:HTTPS_PROXY) {
        Get-ChildItem Env:HTTPS_PROXY
    } else {
        Write-Host "`n Env:HTTPS_PROXY not found`n"
    }
}
function Set-Proxy-XXX
{
    $proxy = 'http://localhost:8123'
    $env:HTTP_PROXY = $proxy
    $env:HTTPS_PROXY = $proxy
    $env:NO_PROXY = ".dz11.com,.coldlake.cn,localhost,127.0.0.1"
    Write-Host "`n   OPEN powershell proxy channel!`n"
    Display-Proxy
}
function Clear-Proxy-XXX
{
    Remove-Item env:HTTP_PROXY
    Remove-Item env:HTTPS_PROXY
    Write-Host "`n   CLOSE powershell proxy channel!`n"
    Display-Proxy
}

分类: 默认 标签: 发布于: 2022-06-03 12:12:18, 更新于: 2022-06-03 12:12:18