Start-KasSetup.ps1

<#PSScriptInfo
    .VERSION 1.0.0.1
    .GUID 2f74e636-61ff-44c8-86b4-be3736e623d7
    .AUTHOR chixus
    .COMPANYNAME CHXOFT
    .COPYRIGHT CHXOFT ©2026. All rights reserved.
    .TAGS kaspersky, setup, automation
    .LICENSEURI
    .PROJECTURI
    .ICONURI
    .EXTERNALMODULEDEPENDENCIES
    .REQUIREDSCRIPTS
    .EXTERNALDETAILS
    .DESCRIPTION Requores Admin rights.
.#>


<#
.SYNOPSIS
    Kaspersky setup tool
#>


#Requires -RunAsAdministrator

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{ Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; Exit }

#region Variables
$VerbosePreference = 'SilentlyContinue'
$WarningPreference = 'SilentlyContinue'
$InformationPreference = 'SilentlyContinue'
$ProgressPreference = 'SilentlyContinue'
Set-Location -Path $PSScriptRoot
$k1 = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('R0FKUFUtVVREMTgtM0IySkotNjJDVTI='))
$k2 = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('R0U4NkYtOVdRUk0tS0s1UEctMVpFMlc='))
$k3 = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('NUFQNTUtVUZBVDEtUVVNTk4tN0NVRFo='))
$taskName = "Kaspersky License Renewal"
$avpCom = ""
$editionName = ""
$licenseKey = ""
$LicType = ""
[boolean]$ReInstall = $false
#endregion Variables

#region WindowSize
$pshost = Get-Host
$window = $pshost.UI.RawUI
try
{
    $width = 65
    $height = 50
    $buffer = $window.BufferSize
    $windowSize = $window.WindowSize
    if ($buffer.Width -lt $width) { $buffer.Width = $width }
    if ($buffer.Height -lt $height) { $buffer.Height = $height }
    $window.BufferSize = $buffer
    $windowSize.Width = $width
    $windowSize.Height = $height
    $window.WindowSize = $windowSize
}
catch { Write-Host "Window resized."; Start-Sleep -Seconds 3 }
#endregion WindowSize

#region InFront
Add-Type -Name "Win32TopMost" -Namespace "Win32Functions" -MemberDefinition @"
    [DllImport("kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
"@
 -PassThru | Out-Null
$HWND_TOPMOST = [IntPtr] - 1
$SWP_NOMOVE = 0x0002
$SWP_NOSIZE = 0x0001
$consolePtr = [Win32Functions.Win32TopMost]::GetConsoleWindow()
if ($consolePtr -ne [IntPtr]::Zero) { [Win32Functions.Win32TopMost]::SetWindowPos($consolePtr, $HWND_TOPMOST, 0, 0, 0, 0, $SWP_NOMOVE -bor $SWP_NOSIZE) | Out-Null }
#endregion InFront

#region Fuctions
function Show-Logo
{
    [CmdletBinding()]
    [OutputType([string])]
    param ()
    
    Clear-Host
    Write-Host "`n`t#===============================================#`t" -BackgroundColor Blue -ForegroundColor Yellow
    Write-Host "`t# Kaspersky auto renewal by CHXOFT (c)2026. #`t" -BackgroundColor Blue -ForegroundColor Yellow
    Write-Host "`t#===============================================#`t" -BackgroundColor Blue -ForegroundColor Yellow
    Write-Host "`t Detected Kaspersky setup version: $majorVersion`t`t`n" -BackgroundColor Blue -ForegroundColor Cyan
}
function Get-UserChoice
{
    [OutputType([string])]
    param ()
    
    while ($true)
    {
        Show-Logo
        $renewal = Read-Host -Prompt "`tDo you want automatic license renewal? (y/n)"
        if ($renewal -eq 'y' -or $renewal -eq 'n') { return $renewal }
        Write-Host "`t Please type (Y)es or (N)o then press enter." -ForegroundColor DarkGray
    }
}
#endregion Functions

#region StartupCheck
$SetupPath = "$env:TEMP\premium.exe"
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36"
$session.Cookies.Add((New-Object System.Net.Cookie("kameleoonVisitorCode", "msgzd1nsukv5juan", "/", ".kaspersky.com")))
Invoke-WebRequest -UseBasicParsing -Uri "https://pdc5.pa2.pdc.kaspersky.com/DownloadManagers/98/9869b8fa-bf0a-44e7-a601-983c32adbe7c/kaspersky4win202121.27.6.532en_46537.exe" -OutFile $SetupPath `
                  -WebSession $session `
                  -Headers @{
    "Accept"                    = "text/html,application/xhtml+xml,application/xml;q=0.9,image/jxl,image/avif,image/webp,image/apng,*/*;q=0.8"
    "Accept-Encoding"            = "gzip, deflate, br, zstd"
    "Accept-Language"            = "en-US,en;q=0.8"
    "Referer"                    = "https://www.kaspersky.com/"
    "Sec-Fetch-Dest"            = "document"
    "Sec-Fetch-Mode"            = "navigate"
    "Sec-Fetch-Site"            = "same-site"
    "Sec-Fetch-User"            = "?1"
    "Sec-GPC"                    = "1"
    "Upgrade-Insecure-Requests" = "1"
    "sec-ch-ua"                    = "`"Not:A-Brand`";v=`"8`", `"Chromium`";v=`"156`", `"Brave`";v=`"156`""
    "sec-ch-ua-mobile"            = "?0"
    "sec-ch-ua-platform"        = "`"Windows`""
}

if ($SetupPath)
{
    $fullVersion = (Get-Item $SetupPath).VersionInfo.ProductVersion
    $majorVersion = ($fullVersion -split '\.')[0 .. 1] -join '.'
}
else
{
    Write-Host "File not downloaded !" -ForegroundColor Red
    Start-Sleep 5
    exit
}

$tsk = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($tsk)
{
    $Argum = $tsk.Actions.Arguments
    if ($Argum -match '/add\s+(.*)')
    {
        $licence = $Matches[1].Trim()
        if ($licence -eq $k1) { $LicType = "Standard" }
        elseif ($licence -eq $k2) { $LicType = "Plus" }
        elseif ($licence -eq $k3) { $LicType = "Premiun" }
        else { $licence = "unknown"; $LicType = "unknown" }
    }
    $licence = $null
    $LicType = $null
}
$Installed = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -like "*Kaspersky*" } | Select-Object DisplayName, DisplayVersion, InstallDate
if ($Installed)
{
    $ReInstall = $true
    $name = $Installed.DisplayName[0]
    $instver = ($Installed.DisplayVersion[0] -split '\.')[0 .. 1] -join '.'
    if ($instver -eq $majorVersion)
    {
        Write-Host "Same version ($instver) is already installed." -ForegroundColor Yellow
        Write-Host "If repairing the installation fails,`nfirst remove the old version." -ForegroundColor Blue
        if ($LicType -and ($LicType -ne "Unknown"))
        {
            Write-Host "Autorenewal licence found type:" -ForegroundColor Yellow
            write-host "Kaspersky $LicType" -ForegroundColor Blue
        }
        Write-Host "`nPress any key..." -ForegroundColor DarkGray
        $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    elseif ($instver -lt $majorVersion)
    {
        Clear-Host
        Write-Host " $name version $instver is already installed ! `n" -ForegroundColor Red -BackgroundColor Black
        if ($LicType -and ($LicType -ne "Unknown"))
        {
            Write-Host "Autorenewal licence found type:" -ForegroundColor Yellow
            write-host "Kaspersky $LicType" -ForegroundColor Blue
        }
        Write-Host "`nPress any key..." -ForegroundColor DarkGray
        $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    else
    {
        if (Test-Path -Path $SetupPath) { Remove-Item -Path $SetupPath -Force -ErrorAction SilentlyContinue }
        Write-Host "You already have newer version $instver." -ForegroundColor Red
        Write-Host "If you want to install this version`nfist uninstall current installed.1`n"
        Write-Host "Setup will close automatically in 10 seconds." -ForegroundColor DarkGray
        Start-Sleep -Seconds 10
        exit
    }
    Write-Host "$([char]27)[3J$([char]27)[H$([char]27)[2J" -NoNewline
    [Console]::Clear()
}
#endregion StartupCheck

#region LicenceSet
$renewal = Get-UserChoice
if ($renewal -eq 'y')
{
    while ($true)
    {
        Show-Logo
        Write-Host "`n`tSelect licence version:`t`t`t`t`n"
        Write-Host "`t`t1. Standard" -ForegroundColor Green
        Write-Host "`t`t2. Plus" -ForegroundColor Green
        Write-Host "`t`t3. Premium" -ForegroundColor Green
        Write-Host "`t`tC. To cancel setup." -ForegroundColor Red
        $edition = Read-Host "`n`tEnter your choice (1 - 3)"
        switch ($edition)
        {
            "1" { $editionName = "Kaspersky Standard"; $licenseKey = $k1; break }
            "2" { $editionName = "Kaspersky Plus"; $licenseKey = $k2; break }
            "3" { $editionName = "Kaspersky Premium"; $licenseKey = $k3; break }
            "c" {
                if (Test-Path -Path $SetupPath)
                {
                    Remove-Item -Path $SetupPath -Force -ErrorAction SilentlyContinue
                }
                Write-Host "`t`t`t`t`t`t`n`t`tInstallation canceled !" -ForegroundColor Red
                Start-Sleep -Seconds 3
                exit
            }
            default { Write-Host "`tInvalid choice, please try again." -ForegroundColor DarkGray }
        }
        if ($licenseKey) { break }
    }
    if ($licence -and $licence -ne "unknown") { Write-Host "`n`tPrevious autorenewal licence was $LicType." -ForegroundColor Yellow }
    Write-Host "`t`t`t`t`t`t`n`tSelected version: $editionName`n" -ForegroundColor Green
}
#endregion LicenceSet

#region Installation
if (Get-Process -Name "avp" -ErrorAction SilentlyContinue) { Write-Host "`tExit from Kaspersky to start installation.`n" -ForegroundColor Red }
While (Get-Process -Name "avp" -ErrorAction SilentlyContinue) { Start-Sleep -Seconds 2 }
Write-Host "Starting installation with self-protection disabled." -ForegroundColor Yellow
Start-Process -FilePath $SetupPath -ArgumentList "/pSelfProtection=0"
Write-Host "When installation window appear click on 'Continue', etc" -ForegroundColor DarkCyan
Start-Sleep -Seconds 3
#endregion Installation

#region Registry
Write-Host "Monitoring and updating registry values..." -ForegroundColor Yellow
$regPath = "HKLM:\SOFTWARE\WOW6432Node\KasperskyLab\AVP$majorVersion\environment"
while ($true)
{
    if (Test-Path -Path $regPath)
    {
        $val = Get-ItemProperty -Path $regPath -Name "ProductStatus" -ErrorAction SilentlyContinue
        if ($null -ne $val)
        {
            Set-ItemProperty -Path $regPath -Name "ProductStatus" -Value ""
            Write-Host "`nRegistry key successfully modified !`n" -ForegroundColor Green
            break
        }
    }
    Start-Sleep -Seconds 2
}
#endregion Registry

#region AutoRenewal
if ($renewal -eq 'y')
{
    Write-Host "Locating 'avp.com'..." -ForegroundColor Yellow
    while (-not $avpCom)
    {
        $avpFiles = Get-ChildItem -Path "C:\Program Files (x86)\Kaspersky Lab" -Filter "avp.com" -Recurse -ErrorAction SilentlyContinue |
        ForEach-Object {
            [PSCustomObject]@{
                Path    = $_.FullName
                Version = $_.VersionInfo.FileVersion
            }
        }
        foreach ($file in $avpFiles)
        {
            if ($file.Version -eq $fullVersion) { Write-Host "'avp.com' version $($file.Version) at:" -ForegroundColor Yellow; $avpCom = $($file.Path) }
            else { Start-Sleep -Seconds 5 }
        }
    }
    Write-Host "$avpCom" -ForegroundColor DarkYellow
    Write-Host "`n'avp.com' located successfully !`n" -ForegroundColor Green
    $action = New-ScheduledTaskAction -Execute "$avpCom" -Argument "LICENSE /add $licenseKey"
    $trigger = New-ScheduledTaskTrigger -Daily -At 9:00AM -DaysInterval 28
    $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries:$false -RunOnlyIfNetworkAvailable
    Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -Force | Out-Null
    
    if (!$ReInstall)
    {
        Write-Host "Waiting for Kaspersky to start up in the background..." -ForegroundColor Yellow
        while (!(Get-Process -Name "avp" -ErrorAction SilentlyContinue)) { Start-Sleep -Seconds 5 }
    }
    else
    {
        Write-Host "Reboot computer if reinstall is done and Kaspersky is asking it." -ForegroundColor Yellow
        Start-Sleep 5
    }
    Start-ScheduledTask -TaskName $taskName
    Write-Host "`nLicense task successfully created and started !`n" -ForegroundColor Green
}
#endregion AutoRenewal

#region Finish
if (Test-Path -Path $SetupPath) { Remove-Item -Path $SetupPath -Force -ErrorAction SilentlyContinue }
Write-Host "`tThe process has been successfully completed !`n" -ForegroundColor Cyan
Write-Host "`t`tPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
#endregion Finish
# SIG # Begin signature block
# MIIFdQYJKoZIhvcNAQcCoIIFZjCCBWICAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU1gRSPTqLkX0RLIXGMu2OxqtK
# CEqgggMVMIIDETCCAfmgAwIBAgIQH/fV3s/n86JKWrCURn6tJTANBgkqhkiG9w0B
# AQsFADAVMRMwEQYDVQQDDApDSFhPRlRjZXJ0MB4XDTI2MTEwMTA4NTEwMloXDTI3
# MTEwMTA5MTEwMlowFTETMBEGA1UEAwwKQ0hYT0ZUY2VydDCCASIwDQYJKoZIhvcN
# AQEBBQADggEPADCCAQoCggEBALOe+6UIdQJShdT4XX5T1w20OCVWxzOQJMv5eezi
# gnsvqz3UDgtyOv4/wiLoPhcqHjJ91WVsTxCiLh/BJ0OersZKMDwbOYs2Wq/CrxHS
# Siwo68W/qGIORujmUct4p97cI5fxKN/9IeJCbHQ0+VCxQZDYkCtX3HdpnmoBiQd1
# /Hwf8assdxRVAqztgPVVJ1Z7Lx5IyL/fUPCI+L5hspdrudOtXfmzvoCLusBKEeLz
# TrcJbOGrCvaXYsCap4AT0FJqPkOhGOzp6+3INklvusdusfJ99jeaDCbjhVD1DS+k
# KQ7x7c4TNR6Ky3v8Iqs/PBg0jGvI9UeFktkXNHvtOKv+Pi0CAwEAAaNdMFswDgYD
# VR0PAQH/BAQDAgeAMBMGA1UdJQQMMAoGCCsGAQUFBwMDMBUGA1UdEQQOMAyCCkNI
# WE9GVGNlcnQwHQYDVR0OBBYEFGQ7X0CR7eWFlh8JOvS5gDN28KckMA0GCSqGSIb3
# DQEBCwUAA4IBAQBqccAhX7Ci63H7aSTxYVM4h9krn5gfnHBNCSOC4aOkd1J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