163 lines
6.0 KiB
Batchfile
163 lines
6.0 KiB
Batchfile
|
|
@echo off
|
||
|
|
setlocal enabledelayedexpansion
|
||
|
|
|
||
|
|
:: Check for administrator privileges
|
||
|
|
net session >nul 2>&1
|
||
|
|
if %errorlevel% neq 0 (
|
||
|
|
echo Requesting administrator privileges...
|
||
|
|
powershell -Command "Start-Process '%~f0' -Verb RunAs"
|
||
|
|
exit /b
|
||
|
|
)
|
||
|
|
|
||
|
|
title PowerShell 7 Auto Setup
|
||
|
|
|
||
|
|
echo.
|
||
|
|
echo ========================================================
|
||
|
|
echo PowerShell 7 Auto Setup
|
||
|
|
echo Automatic Installation and Configuration
|
||
|
|
echo ========================================================
|
||
|
|
echo.
|
||
|
|
|
||
|
|
:: Check if PowerShell 7 is already installed
|
||
|
|
where pwsh >nul 2>nul
|
||
|
|
if %errorlevel% equ 0 (
|
||
|
|
echo [OK] PowerShell 7 is already installed
|
||
|
|
for /f "tokens=*" %%i in ('pwsh -NoProfile -Command "$PSVersionTable.PSVersion.ToString()"') do set PWSH_VERSION=%%i
|
||
|
|
echo [INFO] Version: !PWSH_VERSION!
|
||
|
|
echo.
|
||
|
|
goto :configure
|
||
|
|
)
|
||
|
|
|
||
|
|
echo [INFO] PowerShell 7 not found. Starting installation...
|
||
|
|
echo.
|
||
|
|
|
||
|
|
:: Method 1: Try winget (Windows 10 1809+)
|
||
|
|
echo [1/3] Trying winget installation...
|
||
|
|
winget --version >nul 2>nul
|
||
|
|
if %errorlevel% equ 0 (
|
||
|
|
echo [INFO] Installing via winget...
|
||
|
|
winget install --id Microsoft.PowerShell --silent --accept-package-agreements --accept-source-agreements
|
||
|
|
if %errorlevel% equ 0 (
|
||
|
|
echo [OK] PowerShell 7 installed successfully via winget
|
||
|
|
goto :verify_install
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
:: Method 2: Try Chocolatey
|
||
|
|
echo [2/3] Trying Chocolatey installation...
|
||
|
|
where choco >nul 2>nul
|
||
|
|
if %errorlevel% equ 0 (
|
||
|
|
echo [INFO] Installing via Chocolatey...
|
||
|
|
choco install powershell-core -y
|
||
|
|
if %errorlevel% equ 0 (
|
||
|
|
echo [OK] PowerShell 7 installed successfully via Chocolatey
|
||
|
|
goto :verify_install
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
:: Method 3: Direct MSI download
|
||
|
|
echo [3/3] Downloading PowerShell 7 MSI installer...
|
||
|
|
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference = 'SilentlyContinue'; $latestRelease = Invoke-RestMethod 'https://api.github.com/repos/PowerShell/PowerShell/releases/latest'; $msiAsset = $latestRelease.assets | Where-Object { $_.name -like '*win-x64.msi' } | Select-Object -First 1; if ($msiAsset) { Write-Host '[INFO] Downloading' $msiAsset.name; Invoke-WebRequest -Uri $msiAsset.browser_download_url -OutFile '%TEMP%\PowerShell-7.msi'; exit 0; } else { Write-Host '[ERROR] Failed to find MSI asset'; exit 1; }"
|
||
|
|
|
||
|
|
if %errorlevel% neq 0 (
|
||
|
|
echo [ERROR] Failed to download PowerShell 7
|
||
|
|
echo.
|
||
|
|
echo Please install manually from: https://github.com/PowerShell/PowerShell/releases
|
||
|
|
pause
|
||
|
|
exit /b 1
|
||
|
|
)
|
||
|
|
|
||
|
|
echo [INFO] Installing PowerShell 7...
|
||
|
|
msiexec /i "%TEMP%\PowerShell-7.msi" /quiet /norestart ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ENABLE_PSREMOTING=1 REGISTER_MANIFEST=1
|
||
|
|
set INSTALL_EXIT=%errorlevel%
|
||
|
|
del "%TEMP%\PowerShell-7.msi" >nul 2>&1
|
||
|
|
|
||
|
|
if %INSTALL_EXIT% neq 0 (
|
||
|
|
echo [ERROR] Installation failed with exit code %INSTALL_EXIT%
|
||
|
|
pause
|
||
|
|
exit /b 1
|
||
|
|
)
|
||
|
|
|
||
|
|
:verify_install
|
||
|
|
echo.
|
||
|
|
echo [INFO] Verifying installation...
|
||
|
|
timeout /t 2 /nobreak >nul
|
||
|
|
|
||
|
|
where pwsh >nul 2>nul
|
||
|
|
if %errorlevel% neq 0 (
|
||
|
|
echo [ERROR] PowerShell 7 installation verification failed
|
||
|
|
echo [INFO] Trying to refresh PATH...
|
||
|
|
set "PATH=%PATH%;C:\Program Files\PowerShell\7"
|
||
|
|
where pwsh >nul 2>nul
|
||
|
|
if %errorlevel% neq 0 (
|
||
|
|
echo [ERROR] Still cannot find pwsh.exe
|
||
|
|
echo [INFO] Please restart your computer and try again
|
||
|
|
pause
|
||
|
|
exit /b 1
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
for /f "tokens=*" %%i in ('pwsh -NoProfile -Command "$PSVersionTable.PSVersion.ToString()"') do set PWSH_VERSION=%%i
|
||
|
|
echo [OK] PowerShell 7 installed successfully
|
||
|
|
echo [INFO] Version: !PWSH_VERSION!
|
||
|
|
echo.
|
||
|
|
|
||
|
|
:configure
|
||
|
|
echo ========================================================
|
||
|
|
echo Configuring Default Terminal Settings...
|
||
|
|
echo ========================================================
|
||
|
|
echo.
|
||
|
|
|
||
|
|
:: Get PowerShell 7 installation path
|
||
|
|
for /f "tokens=*" %%i in ('where pwsh') do set PWSH_PATH=%%i
|
||
|
|
echo [INFO] PowerShell 7 path: !PWSH_PATH!
|
||
|
|
echo.
|
||
|
|
|
||
|
|
:: Configure Windows Terminal default profile (if installed)
|
||
|
|
echo [1/4] Configuring Windows Terminal...
|
||
|
|
reg query "HKCU\Console\%%Startup" >nul 2>&1
|
||
|
|
if %errorlevel% equ 0 (
|
||
|
|
reg add "HKCU\Console\%%Startup" /v DelegationConsole /t REG_SZ /d "{574e775e-4f2a-5b96-ac1e-a2962a402336}" /f >nul 2>&1
|
||
|
|
reg add "HKCU\Console\%%Startup" /v DelegationTerminal /t REG_SZ /d "{574e775e-4f2a-5b96-ac1e-a2962a402336}" /f >nul 2>&1
|
||
|
|
echo [OK] Windows Terminal configured
|
||
|
|
) else (
|
||
|
|
echo [SKIP] Windows Terminal not found
|
||
|
|
)
|
||
|
|
|
||
|
|
:: Set PowerShell 7 as default for .ps1 files
|
||
|
|
echo [2/4] Configuring file associations...
|
||
|
|
assoc .ps1=Microsoft.PowerShellScript.1 >nul 2>&1
|
||
|
|
ftype Microsoft.PowerShellScript.1="!PWSH_PATH!" -NoLogo -ExecutionPolicy Bypass -File "%%1" %%* >nul 2>&1
|
||
|
|
echo [OK] .ps1 files associated with PowerShell 7
|
||
|
|
|
||
|
|
:: Add PowerShell 7 to App Paths (for Win+R)
|
||
|
|
echo [3/4] Configuring Win+R shortcut...
|
||
|
|
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\pwsh.exe" /ve /t REG_SZ /d "!PWSH_PATH!" /f >nul 2>&1
|
||
|
|
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\powershell.exe" /ve /t REG_SZ /d "!PWSH_PATH!" /f >nul 2>&1
|
||
|
|
echo [OK] Win+R configured (type 'pwsh' or 'powershell')
|
||
|
|
|
||
|
|
:: Set PowerShell 7 as default shell for developers
|
||
|
|
echo [4/4] Configuring developer settings...
|
||
|
|
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v DefaultTerminalApplication /t REG_SZ /d "{574e775e-4f2a-5b96-ac1e-a2962a402336}" /f >nul 2>&1
|
||
|
|
echo [OK] Developer settings configured
|
||
|
|
|
||
|
|
echo.
|
||
|
|
echo ========================================================
|
||
|
|
echo Installation and Configuration Complete!
|
||
|
|
echo ========================================================
|
||
|
|
echo.
|
||
|
|
echo [OK] PowerShell 7 installed: !PWSH_VERSION!
|
||
|
|
echo [OK] Default terminal configured
|
||
|
|
echo [OK] File associations updated
|
||
|
|
echo [OK] Win+R shortcut configured
|
||
|
|
echo.
|
||
|
|
echo [IMPORTANT] Please restart your computer for all changes
|
||
|
|
echo to take effect, especially Win+R shortcut.
|
||
|
|
echo.
|
||
|
|
echo After restart:
|
||
|
|
echo - Press Win+R and type 'pwsh' or 'powershell'
|
||
|
|
echo - Right-click .ps1 files to run with PowerShell 7
|
||
|
|
echo - Windows Terminal will use PowerShell 7 by default
|
||
|
|
echo.
|
||
|
|
pause
|