Install-1PasswordCLI.ps1


#!/usr/bin/env pwsh

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 0bc79f1b-d4e0-4ec7-a73a-6f0fc386df7f
 
.AUTHOR Jimmy Briggs
 
.COMPANYNAME Jimmy Briggs
 
.COPYRIGHT Jimmy Briggs | 2022
 
.TAGS Installation 1Password
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.PRIVATEDATA
 
#>


<#
 
.SYNOPSIS
    Installs 1Password CLI
.DESCRIPTION
    Installs 1Password CLI (op.exe) from the official download website for 1Password.
.PARAMETER Scope
    The scope of the installation. Can be either 'User' or 'Machine'.
.PARAMETER Arch
    The architecture of the installation. Can be either '64bit' or '32bit'.
.PARAMETER DownloadDir
    The directory to download the installation zip archive to initially; defaults to User's Downloads Directory.
.PARAMETER InstallDir
    The directory to install the 1Password CLI executable to; defaults to 'C:\Program Files\1Password CLI', unless
    the specified `Scope` parameter is "User", then will default to `%USERPROFILE%\bin`.
.PARAMETER AddToPath
    Whether to add the installation directory to the user/machine's PATH environment variable. Defaults to true.
.EXAMPLE
    PS> .\Install-1PasswordCLI.ps1 -Scope User -Arch 64bit
    Installs 1Password CLI for the current user, for the 64bit architecture to the default ~/bin/op.exe location.
.EXAMPLE
    PS> .\Install-1PasswordCLI.ps1 -Scope Machine -Arch 32bit
    Installs 1Password CLI for the current user, for the 32bit architecture to the default C:\Program Files (x86)\1Password CLI\ location.
#>


[CmdletBinding()]
Param(
    [Parameter(HelpMessage = 'Specify to install to the Machine or the Current User Scope.')]
    [ValidateSet('Machine','User')]
    [string]$Scope = 'Machine',

    [parameter(HelpMessage = 'Specify System Architecture. Will default to auto-detected Arch if not provided.')]
    [ValidateSet('64bit', '32bit')]
    [string]$Arch = ((Get-CimInstance Win32_OperatingSystem).OSArchitecture).Replace('-', ''),

    [Parameter(HelpMessage = "Specify the path to download the ZIP Archive to; will default to '%USERPROFILE%\Downloads'.")]
    [ValidateScript({ Test-Path $_ })]
    [string]$DownloadDir = "$env:USERPROFILE\Downloads",

    [Parameter(HelpMessage = "Specify the folder that the Executable will be installed into; will default to 'C:\Program Files\1Password CLI\'.")]
    [string]$InstallDir = "$env:ProgramFiles\\1Password CLI",    

    [Parameter(HelpMessage = "Add to Path? This will ensure the installation directory is included in the User's PATH environment variable.")]
    [System.Boolean]$AddToPath = $true
)

$opArch = switch ($Arch) {
    '64bit' { 'amd64' }
    '32bit' { '386' }
}

$downloadURI = "https://cache.agilebits.com/dist/1P/op2/pkg/v2.4.1/op_windows_$($opArch)_v2.4.1.zip"

$opDownloadDir = Convert-Path -Path $DownloadDir
$opDownloadFile = "$opDownloadDir\op.zip"

$opInstallDir = switch ($InstallDir) {
    'default' {
        switch ($Scope) {
            'Machine' { "$env:ProgramFiles\1Password CLI" }
            'User' { "$env:USERPROFILE\bin" }
        }
    }
    default { Convert-Path -Path $InstallDir }
}

If ($opArch = '386') {
    $opInstallDir = $opInstallDir.Replace('\1Password CLI', ' (x86)\1Password CLI')
}

If (!(Test-Path $opInstallDir)) {
    New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}

Invoke-WebRequest -Uri $downloadURI -OutFile $opDownloadFile
Expand-Archive -Path $opDownloadFile -DestinationPath $opInstallDir -Force

If ($Scope = 'Machine') {
    $envMachinePath = [System.Environment]::GetEnvironmentVariable('PATH','machine')

    If ($envMachinePath -split ';' -notcontains $opInstallDir) {
        [Environment]::SetEnvironmentVariable('PATH', "$envMachinePath;$opInstallDir", 'Machine')
    }
}

If ($Scope = 'User') {
    $envUserPath = [System.Environment]::GetEnvironmentVariable('PATH','user')

    If ($envUserPath -split ';' -notcontains $opInstallDir) {
        [Environment]::SetEnvironmentVariable('PATH', "$envUserPath;$opInstallDir", 'User')
    }

}    

Remove-Item -Path $opDownloadFile -Force

Write-Output "Success! 1Password CLI has been installed to $opInstallDir, and added to the PATH environment variable."