<# Install-LogiSwitch.ps1 - install or update LogiSwitch for the current Windows user (no admin rights). .\Install-LogiSwitch.ps1 # install / update, keep existing settings .\Install-LogiSwitch.ps1 -Edge Left -Channel 3 # also set where the Mac is and its channel #> param( [ValidateSet('Right', 'Left')][string]$Edge, [ValidateRange(1, 3)][int]$Channel, [switch]$NoStart, [switch]$Quiet ) $ErrorActionPreference = 'Stop' $src = Split-Path -Parent $MyInvocation.MyCommand.Path $dst = Join-Path $env:LOCALAPPDATA 'LogiSwitch' $psExe = Join-Path $env:WINDIR 'System32\WindowsPowerShell\v1.0\powershell.exe' $files = 'LogiSwitch.ps1', 'LogiSwitchTray.ps1', 'Install-LogiSwitch.ps1', 'Uninstall-LogiSwitch.ps1', 'Get-LogiSwitchReport.ps1', 'Report.cmd', 'Install.cmd', 'Uninstall.cmd', 'README.txt', 'version.txt', 'ASK-CLAUDE.txt' # 1. Stop any running copy (tray and watcher), but not this installer. Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" | Where-Object { $_.CommandLine -match '\\LogiSwitch(Tray)?\.ps1' -and $_.ProcessId -ne $PID } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } Start-Sleep -Milliseconds 500 # 2. Copy program files. New-Item -ItemType Directory -Force $dst | Out-Null foreach ($f in $files) { $p = Join-Path $src $f if (-not (Test-Path $p)) { continue } if ((Resolve-Path $src).Path -ne (Resolve-Path $dst).Path) { Copy-Item $p $dst -Force } Unblock-File (Join-Path $dst $f) -ErrorAction SilentlyContinue } # 3. Settings: keep existing ones, apply any given on the command line, remember the source folder for updates. $settingsFile = Join-Path $dst 'settings.json' $s = [ordered]@{ Edge = 'Right'; Channel = 2; Active = $true; SourceDir = '' } # Keep EVERY saved setting (also clipboard sync, the Mac's address and port), not only the four above. try { $j = Get-Content $settingsFile -Raw -ErrorAction Stop | ConvertFrom-Json; foreach ($p in $j.PSObject.Properties) { if ($null -ne $p.Value) { $s[$p.Name] = $p.Value } } } catch {} if ($PSBoundParameters.ContainsKey('Edge')) { $s.Edge = $Edge } if ($PSBoundParameters.ContainsKey('Channel')) { $s.Channel = $Channel } if ((Resolve-Path $src).Path -ne (Resolve-Path $dst).Path) { $s.SourceDir = (Resolve-Path $src).Path } [pscustomobject]$s | ConvertTo-Json | Set-Content $settingsFile -Encoding utf8 # 4. Start at login (replaces the older watcher-only shortcut). $startup = [Environment]::GetFolderPath('Startup') Remove-Item (Join-Path $startup 'LogiSwitch edge watcher.lnk') -ErrorAction SilentlyContinue $sc = (New-Object -ComObject WScript.Shell).CreateShortcut((Join-Path $startup 'LogiSwitch.lnk')) $sc.TargetPath = $psExe $sc.Arguments = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$(Join-Path $dst 'LogiSwitchTray.ps1')`"" $sc.WorkingDirectory = $env:LOCALAPPDATA; $sc.WindowStyle = 7; $sc.Description = 'LogiSwitch tray icon' $sc.Save() # 5. Start the tray icon now. if (-not $NoStart) { Start-Process -FilePath $psExe -ArgumentList $sc.Arguments -WorkingDirectory $env:LOCALAPPDATA -WindowStyle Hidden } $ver = try { (Get-Content (Join-Path $dst 'version.txt') | Select-Object -First 1).Trim() } catch { '?' } if (-not $Quiet) { "LogiSwitch $ver installed to $dst" "Mac is to the $($s.Edge.ToUpper()) of this PC; Mac channel = $($s.Channel)." "Look for the green icon beside the clock (click ^ if hidden). Right-click it for options." }