# Installs the Clair agent (clair.exe) on Windows and runs it as a login service. # # irm https://claircode.app/install.ps1 | iex # # Downloads the binary for this system from https://claircode.app/releases, checks it # against that release's SHA256SUMS.txt, puts it in %USERPROFILE%\.local\bin, adds that folder # to your PATH, and runs `clair service install`: a Scheduled Task that starts the agent # when you sign in. Nothing runs elevated. Running it again upgrades in place. # # Environment: # CLAIR_VERSION a specific version, like v0.1.0 (default: the latest) # CLAIR_INSTALL_DIR where the binary goes (default %USERPROFILE%\.local\bin) # CLAIR_NO_SERVICE=1 install the binary only; run `clair service install` yourself # CLAIR_RELEASES_URL where releases are read from (default https://claircode.app/releases) # # macOS and Linux: curl -fsSL https://claircode.app/install.sh | sh # # `iex` runs this in your own session, so everything happens inside one script block that # never calls exit, and an error ends the install without closing the window. & { Set-StrictMode -Version 2 $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' function Get-Setting([string]$Name, [string]$Default) { $value = [Environment]::GetEnvironmentVariable($Name) if ([string]::IsNullOrWhiteSpace($value)) { return $Default } return $value.Trim() } function Get-Arch { if ($env:PROCESSOR_IDENTIFIER -match '^ARM') { return 'arm64' } $name = $null try { $name = [Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() } catch { } if (-not $name) { $name = $env:PROCESSOR_ARCHITECTURE if ($env:PROCESSOR_ARCHITEW6432) { $name = $env:PROCESSOR_ARCHITEW6432 } } switch ($name.ToUpperInvariant()) { 'X64' { return 'amd64' } 'AMD64' { return 'amd64' } 'ARM64' { return 'arm64' } } throw "there is no build for $name. The agent is built for 64-bit Intel and ARM." } function Test-Service { try { return $null -ne (Get-ScheduledTask -TaskName 'clair' -TaskPath '\' -ErrorAction SilentlyContinue) } catch { return $false } } # The user's PATH, kept as an expandable string so entries like %USERPROFILE% survive. function Add-ToUserPath([string]$Dir) { $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true) try { $raw = [string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $entries = @($raw -split ';' | Where-Object { $_ }) if ($entries | Where-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd('\') -ieq $Dir.TrimEnd('\') }) { return $false } $key.SetValue('Path', (@($entries) + $Dir) -join ';', [Microsoft.Win32.RegistryValueKind]::ExpandString) } finally { $key.Close() } # Setting any user variable broadcasts the change, so new terminals see the new PATH. [Environment]::SetEnvironmentVariable('CLAIR_INSTALL_PATH_CHANGED', '1', 'User') [Environment]::SetEnvironmentVariable('CLAIR_INSTALL_PATH_CHANGED', $null, 'User') return $true } function Install-Clair { if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { throw 'this installer is for Windows. On macOS and Linux, run: curl -fsSL https://claircode.app/install.sh | sh' } if ($PSVersionTable.PSVersion.Major -lt 5) { throw "needs PowerShell 5.1 or later; this is $($PSVersionTable.PSVersion)." } # Windows PowerShell 5.1 can default to TLS 1.0, which the download host refuses. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $releases = (Get-Setting 'CLAIR_RELEASES_URL' 'https://claircode.app/releases').TrimEnd('/') if ($releases -notmatch '^https://' -and $releases -notmatch '^http://(localhost|127\.0\.0\.1)[:/]') { throw "CLAIR_RELEASES_URL must be an https:// URL: $releases" } $dir = Get-Setting 'CLAIR_INSTALL_DIR' (Join-Path $env:USERPROFILE '.local\bin') $arch = Get-Arch $tmp = Join-Path ([IO.Path]::GetTempPath()) ('clair-install-' + [Guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $tmp -Force | Out-Null try { $requested = Get-Setting 'CLAIR_VERSION' '' $version = $requested if (-not $version) { try { Invoke-WebRequest -Uri "$releases/latest.txt" -OutFile "$tmp\latest" -UseBasicParsing } catch { throw "couldn't read the latest version from $releases/latest.txt. Check your connection and try again." } $version = (Get-Content -LiteralPath "$tmp\latest" -Raw).Trim() } if ($version -notmatch '^v') { $version = "v$version" } if ($version -notmatch '^v[0-9]') { throw "`"$version`" is not a version." } $asset = "clair-$version-windows-$arch.exe" Write-Host "Downloading clair $version for windows/$arch" try { Invoke-WebRequest -Uri "$releases/$version/$asset" -OutFile "$tmp\clair.exe" -UseBasicParsing Invoke-WebRequest -Uri "$releases/$version/SHA256SUMS.txt" -OutFile "$tmp\SHA256SUMS" -UseBasicParsing } catch { throw "couldn't download clair $version from $releases/$version/." } $want = $null foreach ($line in Get-Content -LiteralPath "$tmp\SHA256SUMS") { $fields = $line.Trim() -split '\s+' if ($fields.Count -eq 2 -and $fields[1].TrimStart('*') -eq $asset) { $want = $fields[0].ToLowerInvariant() } } if (-not $want) { throw "the checksums for $version have no line for $asset." } $got = (Get-FileHash -LiteralPath "$tmp\clair.exe" -Algorithm SHA256).Hash.ToLowerInvariant() if ($got -ne $want) { throw "the download doesn't match its checksum (expected $want, got $got). Nothing was installed." } New-Item -ItemType Directory -Path $dir -Force | Out-Null $dest = Join-Path $dir 'clair.exe' $old = "$dest.old" # An installed service keeps running the old binary; stop it before the swap, and # `clair service install` below starts it again on the new one. if ((Test-Service) -and (Test-Path -LiteralPath $dest)) { # Windows PowerShell turns a native command's redirected stderr into an error. try { & $dest service stop *> $null } catch { } } # A running exe can't be overwritten, but it can be renamed out of the way. Remove-Item -LiteralPath $old -Force -ErrorAction SilentlyContinue if (Test-Path -LiteralPath $dest) { Move-Item -LiteralPath $dest -Destination $old -Force } try { Move-Item -LiteralPath "$tmp\clair.exe" -Destination $dest -Force } catch { if (Test-Path -LiteralPath $old) { Move-Item -LiteralPath $old -Destination $dest -Force } throw } Remove-Item -LiteralPath $old -Force -ErrorAction SilentlyContinue $said = "clair $version" try { $said = & $dest version } catch { } Write-Host "Installed $said at $dest" } finally { Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue } if ((Get-Setting 'CLAIR_NO_SERVICE' '0') -eq '1') { Write-Host "Skipped the service (CLAIR_NO_SERVICE=1). To start it: & '$dest' service install" Write-Host 'To pair your phone: clair pair' } else { & $dest service install if ($LASTEXITCODE -ne 0) { Write-Host '' Write-Host "The agent is installed, but its service didn't start. After fixing what's above, run:" Write-Host " & '$dest' service install" Write-Host 'Then pair your phone: clair pair' } } # A release updates itself to the latest (clair-agent ADR 2026-09.qk6v), which would # undo a version asked for on purpose. if ($requested) { Write-Host '' Write-Host "The agent updates itself to the newest release. To stay on $version, set" Write-Host '"autoUpdate": false in its agent.json.' } if (Add-ToUserPath $dir) { Write-Host '' Write-Host "Added $dir to your PATH. Open a new terminal to use clair there." } if (-not (($env:Path -split ';') | Where-Object { $_.TrimEnd('\') -ieq $dir.TrimEnd('\') })) { $env:Path = "$env:Path;$dir" } } try { Install-Clair } catch { Write-Host "clair install: $($_.Exception.Message)" -ForegroundColor Red } }