powershell7下载和安装@pwsh一键部署@powershell下载加速@国内镜像加速下载安装包

powershell 7下载和安装

介绍多种下载安装powershell的方案,适合国内用户操作

一键部署自动化方案

一行执行

irm 'https://gitee.com/xuchaoxin1375/scripts/raw/main/PS/Deploy/Deploy-Pwsh7Portable.ps1'|iex

打开windows自带的powershell(v5.1),执行上述安装操作(尝试加速下载下载并解压powershell7 编写版安装包zip)

备用方案

如果上述命令失败,那么尝试以下安装脚本的源码,根据需要修改其中的几个参数

源码如下

function Deploy-Pwsh7Portable
{
    <# 
    .SYNOPSIS
    一键安装powershell7 portable(便携版)
    .DESCRIPTION
    要求powershell5.1或以上版本执行此脚本,否则无法保证可以解压并部署安装文件
    这里提供一键安装的方法,试图使用最快捷的操作完成安装或部署
    其他备选值得推荐的方法有国内应用软件镜像站下载安装包,或者应用商店比如联想应用商店下载安装版
    官方推荐的方法国内比较慢或者稳定性不足
    .NOTES
    虽然Microsoft官方推荐使用winget 或者应用商店下载,但是前者从github直接下载不稳定,
    后者从应用商店下载有时候网络问题点击下载会反应比较久,而且安装路径不方便确定
    #>
    [CmdletBinding(SupportsShouldProcess)]
    param (
        # 'https://github.com/PowerShell/PowerShell/releases/latest/download/powershell-7.x.x-win-x64.zip'
        $BaseUrl = '',
        $mirror = 'https://gh-proxy.com',
        # "$env:ProgramFiles\PowerShell\7" #可能要管理员权限
        $InstallPath = "$env:systemDrive\powershell\7"

    )

    function Get-LatestPowerShellDownloadUrl
    {
        <# 
        .SYNOPSIS
        从github获取powershell最新稳定版下载链接,支持指定下载类型(zip/msi)
        #>
        param(
            [ValidateSet('msi', 'zip')]$PackageType = 'msi'
        )
        $releasesUrl = 'https://api.github.com/repos/PowerShell/PowerShell/releases/latest'
        $releaseInfo = Invoke-RestMethod -Uri $releasesUrl -Headers @{ 'User-Agent' = 'PowerShell-Script' }

        Write-Host "Trying to get latest PowerShell ${PackageType}..."
        foreach ($asset in $releaseInfo.assets)
        {
            if ($asset.name -like "*win-x64.${PackageType}")
            {
                return $asset.browser_download_url
            }
        }
        throw 'No suitable installer found in the latest release.'
    }
    # 定义下载 URL 模板
    if ( ! $baseUrl )
    {
        $baseUrl = Get-LatestPowerShellDownloadUrl -PackageType zip
    }
    if ( $mirror )
    {
        Write-Host "try use mirror: $mirror to speed up link"
        $BaseUrl = "$mirror/$BaseUrl".trim('/')
    }
    else
    {
        Write-Host 'Use no mirror'
    }
    $versionCode = $s -replace '.*(\d+\.\d+\.\d+).*', '$1'
    # 下载文件
    $outputPath = "$env:TEMP\powershell-$versionCode.zip"
    if (!(Test-Path -Path $outputPath))
    {

        # 调用 Invoke-WebRequest 下载文件
        Invoke-WebRequest -Uri $baseUrl -OutFile $outputPath
    }
    else
    {
        Write-Output "$outputPath is already exist"
    }

    
    # 确保安装路径存在
    # $InstallPath="$InstallPath\$versionCode"
    if ((Test-Path -Path $installPath))
    {
        # 备份原来的路径
        Rename-Item -Path $InstallPath -NewName "$($InstallPath).bak.$((Get-Date).ToString('yyyy-MM-dd--HH-mm-ss'))" -Verbose
        # 新建一个路径用于全新安装
        New-Item -ItemType Directory -Path $installPath -Verbose
    }
    # 解压文件
    Expand-Archive -Path $outputPath -DestinationPath $installPath -Force 

    # 清理下载的压缩包
    Remove-Item -Path $outputPath -Verbose -Confirm
    
    if ($PSCmdlet.ShouldProcess("$installPath", 'Add to PATH'))
    {
        
        # 将 pwsh.exe 添加到 PATH 环境变量
        # 方案1
        # $UserPath = [Environment]::GetEnvironmentVariable('Path', 'User')
        # $NewUserPath = "$installPath;$UserPath"
        # 方案2
        $RawPathValue = reg query 'HKEY_CURRENT_USER\Environment' /v Path
        $RawPathValue = @($RawPathValue) -join '' #确保$RawPathValue是一个字符串
        # $RawPathValue -match 'Path\s+REG_EXPAND_SZ\s+(.+)'
        $RawPathValue -match 'Path\s+REG.*SZ\s+(.+)'
        $UserPath = $Matches[1] 

        Write-Verbose "Path value of [$env:Username] `n$($UserPath -split ';' -join "`n")" -Verbose
        # 这里仅操作User级别,不需要管理权限
        $NewUserPath = "$InstallPath;$UserPath"

        # 执行添加操作
        [Environment]::SetEnvironmentVariable(
            'Path', $NewUserPath , [System.EnvironmentVariableTarget]::User
        )
        # 更新当前环境变量
        $env:Path = "$InstallPath;$env:path"
        
    }
    $pwshPath = "$installPath\pwsh.exe"
    # 验证安装
    if (Test-Path -Path $pwshPath)
    {
        Write-Output 'PowerShell 7 installed successfully'
    }
    if (Get-Command pwsh -ErrorAction SilentlyContinue)
    {
        Write-Output 'PowerShell 7 was added to the PATH environment variable.'
        Start-Process pwsh
    }
    else
    {
        Write-Output '安装失败,请检查脚本和网络连接。'
    }
    
}
function Deploy-Pwsh7Portable
{
    <# 
    .SYNOPSIS
    一键安装powershell7 portable(便携版)
    .DESCRIPTION
    要求powershell5.1或以上版本执行此脚本,否则无法保证可以解压并部署安装文件
    这里提供一键安装的方法,试图使用最快捷的操作完成安装或部署
    其他备选值得推荐的方法有国内应用软件镜像站下载安装包,或者应用商店比如联想应用商店下载安装版
    官方推荐的方法国内比较慢或者稳定性不足
    .NOTES
    虽然Microsoft官方推荐使用winget 或者应用商店下载,但是前者从github直接下载不稳定,
    后者从应用商店下载有时候网络问题点击下载会反应比较久,而且安装路径不方便确定
    #>
    [CmdletBinding(SupportsShouldProcess)]
    param (
        # 'https://github.com/PowerShell/PowerShell/releases/latest/download/powershell-7.x.x-win-x64.zip'
        $BaseUrl = '',
        $mirror = 'https://gh-proxy.com',
        # "$env:ProgramFiles\PowerShell\7" #可能要管理员权限
        $InstallPath = "$env:systemDrive\powershell\7"

    )

    function Get-LatestPowerShellDownloadUrl
    {
        <# 
        .SYNOPSIS
        从github获取powershell最新稳定版下载链接,支持指定下载类型(zip/msi)
        #>
        param(
            [ValidateSet('msi', 'zip')]$PackageType = 'msi'
        )
        $releasesUrl = 'https://api.github.com/repos/PowerShell/PowerShell/releases/latest'
        $releaseInfo = Invoke-RestMethod -Uri $releasesUrl -Headers @{ 'User-Agent' = 'PowerShell-Script' }

        Write-Host "Trying to get latest PowerShell ${PackageType}..."
        foreach ($asset in $releaseInfo.assets)
        {
            if ($asset.name -like "*win-x64.${PackageType}")
            {
                return $asset.browser_download_url
            }
        }
        throw 'No suitable installer found in the latest release.'
    }
    # 定义下载 URL 模板
    if ( ! $baseUrl )
    {
        $baseUrl = Get-LatestPowerShellDownloadUrl -PackageType zip
    }
    if ( $mirror )
    {
        Write-Host "try use mirror: $mirror to speed up link"
        $BaseUrl = "$mirror/$BaseUrl".trim('/')
    }
    else
    {
        Write-Host 'Use no mirror'
    }
    $versionCode = $s -replace '.*(\d+\.\d+\.\d+).*', '$1'
    # 下载文件
    $outputPath = "$env:TEMP\powershell-$versionCode.zip"
    if (!(Test-Path -Path $outputPath))
    {

        # 调用 Invoke-WebRequest 下载文件
        Invoke-WebRequest -Uri $baseUrl -OutFile $outputPath
    }
    else
    {
        Write-Output "$outputPath is already exist"
    }

    
    # 确保安装路径存在
    # $InstallPath="$InstallPath\$versionCode"
    if ((Test-Path -Path $installPath))
    {
        # 备份原来的路径
        Rename-Item -Path $InstallPath -NewName "$($InstallPath).bak.$((Get-Date).ToString('yyyy-MM-dd--HH-mm-ss'))" -Verbose
        # 新建一个路径用于全新安装
        New-Item -ItemType Directory -Path $installPath -Verbose
    }
    # 解压文件
    Expand-Archive -Path $outputPath -DestinationPath $installPath -Force 

    # 清理下载的压缩包
    Remove-Item -Path $outputPath -Verbose -Confirm
    
    if ($PSCmdlet.ShouldProcess("$installPath", 'Add to PATH'))
    {
        
        # 将 pwsh.exe 添加到 PATH 环境变量
        # 方案1
        # $UserPath = [Environment]::GetEnvironmentVariable('Path', 'User')
        # $NewUserPath = "$installPath;$UserPath"
        # 方案2
        $RawPathValue = reg query 'HKEY_CURRENT_USER\Environment' /v Path
        $RawPathValue = @($RawPathValue) -join '' #确保$RawPathValue是一个字符串
        # $RawPathValue -match 'Path\s+REG_EXPAND_SZ\s+(.+)'
        $RawPathValue -match 'Path\s+REG.*SZ\s+(.+)'
        $UserPath = $Matches[1] 

        Write-Verbose "Path value of [$env:Username] `n$($UserPath -split ';' -join "`n")" -Verbose
        # 这里仅操作User级别,不需要管理权限
        $NewUserPath = "$InstallPath;$UserPath"

        # 执行添加操作
        [Environment]::SetEnvironmentVariable(
            'Path', $NewUserPath , [System.EnvironmentVariableTarget]::User
        )
        # 更新当前环境变量
        $env:Path = "$InstallPath;$env:path"
        
    }
    $pwshPath = "$installPath\pwsh.exe"
    # 验证安装
    if (Test-Path -Path $pwshPath)
    {
        Write-Output 'PowerShell 7 installed successfully'
    }
    if (Get-Command pwsh -ErrorAction SilentlyContinue)
    {
        Write-Output 'PowerShell 7 was added to the PATH environment variable.'
        Start-Process pwsh
    }
    else
    {
        Write-Output '安装失败,请检查脚本和网络连接。'
    }
    
}

#调用函数
 Deploy-Pwsh7Portable

使用scoop for cn来安装powershell7

需要先安装scoop,然后利用scoop 安装git

然后添加bucket,最后安装powershell7

总之这个方案的步骤会多一些(虽然也是几个命令行的事,但是会绕一点路),优先考虑以上方案

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

Invoke-WebRequest -useb scoop.201704.xyz | Invoke-Expression

scoop install git  #为当前用户安装,如果全局需要管理员权限
scoop bucket add main
scoop bucket add spc https://github.moeyy.xyz/https://github.com/lzwme/scoop-proxy-cn
scoop install powershell 
  • 会自动配置环境变量

其他方案

powershell安装包程序加速下载(推荐)

官网下载直接下载

获取github release下载连接后用镜像加速下载

windows 应用商店直接安装

  • 可以通过windows应用商店安装
    • 但是我不推荐
    • 会安装在个人用户目录中
    • 不利于多用户共享!
  • 在这里插入图片描述

通过msi安装包安装😊

老系统更新powershell

默认安装目录

系统自带的powershell

  • C:\WINDOWS\System32\WindowsPowerShell\v1.0

安装包安装

查看当前pwsh安装路径的方法

  • 检查$psHome即可
PS C:\Users\cxxu> $PSHOME
C:\Program Files\PowerShell\7

商店版安装后所在目录参考

  • C:\Users\<your userame>\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe\pwsh.exe

乱码问题😊

terminal推荐

v7.0.0-preview.4 - 09/19/2019 Engine Updates and Fixes Add support to ActionPreference.Break to break into debugger when Debug, Error, Information, Progress, Verbose or Warning messages are generated (#8205) (Thanks @KirkMunro!) Enable starting control panel add-ins within PowerShell Core without specifying .CPL extension. (#9828) Performance Make ForEach-Object faster for its commonly used scenarios (#10454) and fix ForEach-Object -Parallel performance problem with many runspaces (#10455) Experimental Features Update PSDesiredStateConfiguration module version to 2.0.3 and bring new tests; enable compilation to MOF on non-Windows and use of Invoke-DSCResource without LCM (#10516) Add APIs for breakpoint management in runspaces and enable attach to process without BreakAll for PowerShell Editor Services (#10338) (Thanks @KirkMunro!) Support ternary operator in PowerShell language (#10367) General Cmdlet Updates and Fixes Add PowerShell Core group policy definitions (#10468) Update console host to support XTPUSHSGR/XTPOPSGR VT control sequences that are used in composability scenarios. (#10208) Add WorkingDirectory parameter to Start-Job (#10324) (Thanks @davinci26!) Remove the event handler that was causing breakpoint changes to be erroneously replicated to the host runspace debugger (#10503) (Thanks @KirkMunro!) Replace api-ms-win-core-job-12-1-0.dll with Kernell32.dll in Microsoft.PowerShell.Commands.NativeMethods P/Invoke API(#10417) (Thanks @iSazonov!) Fix wrong output for New-Service in variable assignment and -OutVariable (#10444) (Thanks @kvprasoon!) Fix global tool issues around exit code, command line parameters and path with spaces (#10461) Fix recursion into OneDrive - change FindFirstFileEx() to use SafeFindHandle type (#10405) Skip auto-loading PSReadLine on Windows if the NVDA screen reader is active (#10385) Increase built-with-PowerShell module versions to 7.0.0.0 (#10356) Add throwing an error in Add-Type if a type with the same name already exists (#9609) (Thanks @iSazonov!)
<think>好的,用户的问题是关于在PowerShell安装zip压缩工具的教程。首先,我需要理解用户的需求。他们可能希望在PowerShell环境下使用zip相关的功能,比如压缩或解压文件。根据提供的引用内容,特别是引用[3],提到PowerShell从v5开始内置了压缩解压zip的命令,所以可能不需要额外安装工具。但用户可能不清楚这一点,或者需要更详细的步骤。 接下来,我要检查提供的引用资料。引用[1][2]提到了7Zip4Powershell,这是一个第三方模块,支持更多格式。引用[3]指出PowerShell自v5起支持ZipArchive类,可以使用Compress-ArchiveExpand-Archive命令。引用[4]提到了使用Expand-Archive解压安装其他工具,比如GnuWin中的unzip。 用户可能有两种需求:一种是使用内置的PowerShell命令,另一种是安装第三方工具如7-Zip或GnuWin的unzip。需要分情况回答,先说明内置方法,再介绍第三方工具安装。 需要确保步骤清晰,括检查PowerShell版本、使用内置命令的示例,以及安装7Zip4Powershell模块的步骤。同时,要提到可能的错误解决方法,比如版本过低或模块安装失败。 另外,用户可能混淆了“安装zip工具”“在PowerShell中使用zip功能”,需要明确区分。例如,内置命令不需要安装,而第三方工具需要。还要注意引用对应的参考资料,比如引用[3][1]、[2]。 最后,生成相关问题时要考虑用户可能的后续需求,比如处理其他压缩格式或优化压缩性能。</think>### PowerShell 安装/使用 ZIP 压缩工具教程 #### 一、使用 PowerShell 原生压缩功能(无需安装PowerShell 5.0+ 内置了 $Compress-Archive$ $Expand-Archive$ 命令,支持 ZIP 格式的压缩与解压[^3]: 1. **检查 PowerShell 版本**: ```powershell $PSVersionTable.PSVersion ``` *若版本低于 5.0,需升级至 Windows Management Framework 5.1* 2. **压缩文件夹**: ```powershell Compress-Archive -Path "C:\source_folder" -DestinationPath "C:\output.zip" ``` 3. **解压文件**: ```powershell Expand-Archive -Path "C:\input.zip" -DestinationPath "C:\target_folder" ``` --- #### 二、安装第三方 ZIP 工具(支持更多格式) ##### 方案1:安装 7-Zip 模块 通过 7Zip4Powershell 模块可实现 7z/ZIP/GZIP 等格式操作[^1][^2]: 1. **安装模块**: ```powershell Install-Module -Name 7Zip4Powershell -Scope CurrentUser -Force ``` 2. **压缩文件**: ```powershell Compress-7Zip -ArchiveFileName "output.7z" -Path "C:\source_folder" -Format SevenZip ``` 3. **解压文件**: ```powershell Expand-7Zip -ArchiveFileName "input.zip" -TargetPath "C:\target_folder" ``` ##### 方案2:安装 GNUWin32 工具 通过 GnuWin32 获取传统 UNIX 工具链[^4]: 1. **下载安装**: ```powershell Invoke-WebRequest -Uri "https://downloads.sourceforge.net/gnuwin32/unzip-5.51-1-bin.zip" -OutFile "unzip.zip" ``` 2. **解压并配置环境变量**: ```powershell Expand-Archive -Path "unzip.zip" -DestinationPath "$env:ProgramFiles\GnuWin32" $env:Path += ";$env:ProgramFiles\GnuWin32\bin" ``` --- #### 三、常见问题解决 1. **模块安装失败**: ```powershell Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # 开启脚本执行权限 ``` 2. **路径含空格需加引号**: ```powershell Compress-Archive -Path "'C:\My Documents'" -DestinationPath "C:\output.zip" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cxxu1375

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值