| 1 | #requires -Version 5.1 |
| 2 | <# |
| 3 | .SYNOPSIS |
| 4 | Inspect, install or launch the official x64 Claude Desktop MSIX as the current user. |
| 5 | .DESCRIPTION |
| 6 | Run with 64-bit Windows PowerShell inside the target VM. For Install and Launch, |
| 7 | pass the Id obtained independently from Get-VM on the Hyper-V host. |
| 8 | The full MSIX includes a service. Register it in an elevated session for the |
| 9 | target guest user, or provision it in the guest image before that user signs in. |
| 10 | Launch in the user's normal interactive session. |
| 11 | Download the local MSIX from https://support.claude.com/en/articles/12622703 . |
| 12 | #> |
| 13 | [CmdletBinding()] |
| 14 | param( |
| 15 | [ValidateSet('Inspect', 'Install', 'Launch')][string]$Action = 'Inspect', |
| 16 | [Guid]$ExpectedVMId = [Guid]::Empty, |
| 17 | [string]$MsixPath |
| 18 | ) |
| 19 | $ErrorActionPreference = 'Stop' |
| 20 | |
| 21 | # Verified against AppxManifest.xml and AppxSignature.p7x in the official |
| 22 | # Windows x64 4.23118.0.0 MSIX. The package also contains helper applications. |
| 23 | $publisher = 'CN="Anthropic, PBC", O="Anthropic, PBC", L=San Francisco, S=California, C=US, SERIALNUMBER=4860621, OID.2.5.4.15=Private Organization, OID.1.3.6.1.4.1.311.60.2.1.2=Delaware, OID.1.3.6.1.4.1.311.60.2.1.3=US' |
| 24 | $packageFamily = 'Claude_pzs8sxrjxfjjc' |
| 25 | |
| 26 | function Assert-TargetGuest([Guid]$expected) { |
| 27 | if ($expected -eq [Guid]::Empty) { |
| 28 | throw 'Pass -ExpectedVMId with the target VM Id from Get-VM on the Hyper-V host.' |
| 29 | } |
| 30 | $system = Get-CimInstance Win32_ComputerSystem |
| 31 | if ($system.Manufacturer -ne 'Microsoft Corporation' -or $system.Model -ne 'Virtual Machine') { |
| 32 | throw 'This operation requires a Hyper-V guest.' |
| 33 | } |
| 34 | $kvp = Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' -Name VirtualMachineId |
| 35 | $actual = [Guid]::Empty |
| 36 | if (![Guid]::TryParse($kvp.VirtualMachineId, [ref]$actual) -or $expected -ne $actual) { |
| 37 | throw 'The Hyper-V guest VM Id does not match the host-provided target.' |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function Assert-ClaudeManifest([xml]$manifest) { |
| 42 | $ns = [Xml.XmlNamespaceManager]::new($manifest.NameTable) |
| 43 | $ns.AddNamespace('m', 'http://schemas.microsoft.com/appx/manifest/foundation/windows10') |
| 44 | $identity = $manifest.SelectSingleNode('/m:Package/m:Identity', $ns) |
| 45 | $app = $manifest.SelectSingleNode('/m:Package/m:Applications/m:Application[@Id="Claude"]', $ns) |
| 46 | if (!$identity -or $identity.GetAttribute('Name') -cne 'Claude' -or |
| 47 | $identity.GetAttribute('Publisher') -cne $publisher -or |
| 48 | $identity.GetAttribute('ProcessorArchitecture') -ne 'x64' -or !$app -or |
| 49 | $app.GetAttribute('Executable') -ine 'app\Claude.exe' -or |
| 50 | $app.GetAttribute('EntryPoint') -cne 'Windows.FullTrustApplication') { |
| 51 | throw 'Unexpected Claude x64 package identity or application entry point.' |
| 52 | } |
| 53 | return [version]$identity.GetAttribute('Version') |
| 54 | } |
| 55 | |
| 56 | function Assert-ClaudeSignature($signature) { |
| 57 | if ($signature.Status -ne 'Valid' -or !$signature.SignerCertificate) { |
| 58 | throw 'Windows did not validate the MSIX signature.' |
| 59 | } |
| 60 | # Decode both subjects with the same Windows formatter, including the |
| 61 | # organization registration number. A matching common name alone is insufficient. |
| 62 | $expected = [Security.Cryptography.X509Certificates.X500DistinguishedName]::new($publisher) |
| 63 | $format = [Security.Cryptography.X509Certificates.X500DistinguishedNameFlags]::Reversed |
| 64 | if ($signature.SignerCertificate.SubjectName.Decode($format) -cne $expected.Decode($format)) { |
| 65 | throw 'The trusted signing certificate does not match the Anthropic publisher.' |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function Get-RegisteredClaude { |
| 70 | $packages = @(Get-AppxPackage -Name Claude | Where-Object PackageFamilyName -ceq $packageFamily) |
| 71 | if ($packages.Count -eq 0) { return } |
| 72 | if ($packages.Count -ne 1) { throw 'Multiple registrations found for the Claude package family.' } |
| 73 | $pkg = $packages[0] |
| 74 | if ($pkg.Publisher -cne $publisher -or $pkg.IsDevelopmentMode -or $pkg.IsPartiallyStaged -or |
| 75 | $pkg.SignatureKind -notin 'Developer','Store','Enterprise' -or $pkg.Status -ne 'Ok') { |
| 76 | throw 'The registered Claude package is unsigned, unhealthy or has an unexpected publisher.' |
| 77 | } |
| 78 | $version = Assert-ClaudeManifest (Get-AppxPackageManifest -Package $pkg.PackageFullName) |
| 79 | return [pscustomobject]@{ |
| 80 | Registered = $true |
| 81 | Version = $version.ToString() |
| 82 | Aumid = "$packageFamily!Claude" |
| 83 | Executable = Join-Path $pkg.InstallLocation 'app\Claude.exe' |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function Get-VerifiedClaudeMsixVersion([IO.FileInfo]$file, [IO.Stream]$handle) { |
| 88 | Assert-ClaudeSignature (Get-AuthenticodeSignature -LiteralPath $file.FullName) |
| 89 | Add-Type -AssemblyName System.IO.Compression |
| 90 | $zip = [IO.Compression.ZipArchive]::new($handle, 'Read', $true) |
| 91 | try { |
| 92 | $entry = $zip.GetEntry('AppxManifest.xml') |
| 93 | if (!$entry) { throw 'The MSIX is missing AppxManifest.xml.' } |
| 94 | $stream = $entry.Open() |
| 95 | try { |
| 96 | $manifest = [xml]::new() |
| 97 | $manifest.XmlResolver = $null |
| 98 | $manifest.Load($stream) |
| 99 | return (Assert-ClaudeManifest $manifest) |
| 100 | } finally { $stream.Dispose() } |
| 101 | } finally { $zip.Dispose() } |
| 102 | } |
| 103 | |
| 104 | function Install-ClaudeMsix([string]$path) { |
| 105 | if (!$path) { throw 'Install requires -MsixPath with a locally downloaded x64 MSIX.' } |
| 106 | $file = Get-Item -LiteralPath $path |
| 107 | if ($file -isnot [IO.FileInfo] -or $file.Extension -ne '.msix' -or |
| 108 | $file.FullName.StartsWith('\\') -or |
| 109 | [IO.DriveInfo]::new($file.Directory.Root.FullName).DriveType -eq 'Network') { |
| 110 | throw 'Select a local .msix file on the guest.' |
| 111 | } |
| 112 | # Keep the file read-locked while Windows checks and registers the package. |
| 113 | $handle = [IO.File]::Open($file.FullName, 'Open', 'Read', 'Read') |
| 114 | try { |
| 115 | $version = Get-VerifiedClaudeMsixVersion $file $handle |
| 116 | $registered = Get-RegisteredClaude |
| 117 | if ($registered -and [version]$registered.Version -ge $version) { return $registered } |
| 118 | try { Add-AppxPackage -Path $file.FullName } catch { |
| 119 | if ($_.Exception.HResult.ToString('X8') -eq '80073D28' -or $_.ToString() -match '\b0x80073D28\b') { |
| 120 | throw '0x80073D28: The Claude MSIX service requires guest administrator rights. Register it elevated as this guest user, or provision it in the guest image for standard users.' |
| 121 | } |
| 122 | throw |
| 123 | } |
| 124 | $registered = Get-RegisteredClaude |
| 125 | if (!$registered -or [version]$registered.Version -lt $version) { |
| 126 | throw 'Windows did not register the expected version for this user.' |
| 127 | } |
| 128 | return $registered |
| 129 | } finally { $handle.Dispose() } |
| 130 | } |
| 131 | |
| 132 | function Open-ClaudeWindow($registered) { |
| 133 | if (!$registered) { throw 'Claude Desktop is not registered for this user.' } |
| 134 | $session = [Diagnostics.Process]::GetCurrentProcess().SessionId |
| 135 | $shell = Get-Process -Name explorer -ErrorAction SilentlyContinue | Where-Object SessionId -eq $session |
| 136 | if ($session -eq 0 -or !$shell -or ![Environment]::UserInteractive) { |
| 137 | throw 'Launch requires a signed-in desktop session. Open PowerShell in the guest console.' |
| 138 | } |
| 139 | $explorer = Join-Path ([Environment]::GetFolderPath('Windows')) 'explorer.exe' |
| 140 | Start-Process -FilePath $explorer -ArgumentList ('shell:AppsFolder\' + $registered.Aumid) |
| 141 | $timer = [Diagnostics.Stopwatch]::StartNew() |
| 142 | do { |
| 143 | $window = Get-Process -Name Claude -ErrorAction SilentlyContinue | Where-Object { |
| 144 | $_.SessionId -eq $session -and $_.MainWindowHandle -ne 0 -and $_.Path -eq $registered.Executable |
| 145 | } | Select-Object -First 1 |
| 146 | if ($window) { |
| 147 | return [pscustomobject]@{ Aumid = $registered.Aumid; WindowProcessId = $window.Id } |
| 148 | } |
| 149 | Start-Sleep -Milliseconds 400 |
| 150 | } while ($timer.Elapsed.TotalSeconds -lt 30) |
| 151 | throw 'Activation was requested, but no window from the registered Claude executable appeared within 30 seconds.' |
| 152 | } |
| 153 | |
| 154 | if ($MyInvocation.InvocationName -eq '.') { return } # Allow isolated validation tests to load the checks. |
| 155 | if ([Environment]::OSVersion.Platform -ne 'Win32NT' -or ![Environment]::Is64BitProcess) { |
| 156 | throw 'Run this script in 64-bit Windows PowerShell.' |
| 157 | } |
| 158 | if ($Action -ne 'Install' -and $MsixPath) { throw 'Only Install accepts -MsixPath.' } |
| 159 | if ($Action -ne 'Inspect') { Assert-TargetGuest $ExpectedVMId } |
| 160 | switch ($Action) { |
| 161 | 'Inspect' { |
| 162 | $registered = Get-RegisteredClaude |
| 163 | if (!$registered) { $registered = [pscustomobject]@{ Registered = $false } } |
| 164 | $registered | ConvertTo-Json |
| 165 | } |
| 166 | 'Install' { Install-ClaudeMsix $MsixPath | ConvertTo-Json } |
| 167 | 'Launch' { Open-ClaudeWindow (Get-RegisteredClaude) | ConvertTo-Json } |
| 168 | } |