<# 
Setup.ps1
 
Uninstalls* all versions of DropBox and installs latest
 
* Dropbox does not install for all users, it installs for the installing user, and that
user requires administrative access... this is a problem.
 
Script uses 7-Zip (installed on all of our machines) to unpack the installer into the
desired destination directory, and then creates the necessary registry entries and
Start Menu shortcut.
 
Script does not load each registry hive and attempt to uninstall per-user installs.
 
OS: W8, W7, XP
Arch: x86, x64
 
Adam Sailer
2013.05.09
#>
 
 
## Globals
$invoke = split-path -path $myinvocation.mycommand.path -parent
$os = gwmi win32_OperatingSystem
$proc = gwmi win32_Processor
$debug = $false
 
 
$dest = $env:ProgramFiles
$key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
 
if (($proc.AddressWidth -eq 64) -and ($proc.DataWidth -eq 64))
{
    $dest = ${env:ProgramFiles(x86)}
    $key = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'     
}
 
 
Function KillApps
{
    write-host "`n`n@@ Called KillApps" -fore Magenta
    get-process -name *dropbox* | stop-process -force
}
 
 
Function Unpack
{
    write-host "`n`n@@ Called Unpack" -fore Magenta
 
    $7z = "$env:ProgramFiles\7-Zip\7z.exe"
    if (!(test-path $7z)) { exit 1 }
 
    $archives = (dir -path $invoke\setup -recurse -include Dropbox*.exe)
 
    foreach ($archive in $archives)
    {
        write "Unpacking : `"$archive`""
        $options = "x -o`"$dest\Dropbox`" `"$archive`" -y"
         
        if (!$debug)
        {         
            $process = (Start-Process -FilePath $7z -ArgumentList $options -Wait -Passthru)
            if ($process.ExitCode) { write $process.ExitCode; exit $process.ExitCode }
        }  
        else { write $options }
    }
}
 
 
Function Setup
{
    write-host "`n`n@@ Called Setup" -for Magenta
 
    $apps = @(dir -path $dest\Dropbox -recurse -include dropbox.exe)
 
    $path = "$env:AllUsersProfile\Start Menu\Programs"
 
    foreach ($app in $apps)
    {
        ## $app | select-object *
        $product = $app.VersionInfo.ProductName
        $version = $app.VersionInfo.ProductVersion
 
        $wshell = New-Object -comObject WScript.Shell
         
        $link = $wshell.CreateShortcut("$path\$product.lnk")
        $link.Description = $product
        $link.TargetPath = $app
        $link.IconLocation = $app
         
        if (!$debug) { $link.Save() }
         
         
        #### Registry Entries
 
        $item = "$key\$product"
        new-item -path $key -name $product -force | out-null
 
        set-itemProperty -path $item -name DisplayIcon -value "$app,0" -force
        set-itemProperty -path $item -name DisplayName -value $product -force
        set-itemProperty -path $item -name DisplayVersion -value $version -force
        set-itemProperty -path $item -name Path_Directory -value "$dest\$product" -force
        set-itemProperty -path $item -name Path_Shortcut -value "$path\$product.lnk" -force
        set-itemProperty -path $item -name Path_Menu -value "$path\$product" -force
        set-itemProperty -path $item -name Publisher -value "$product, Inc." -force
        set-itemProperty -path $item -name UninstallString -value 'Use PowerShell script' -force
 
        gp $item
    }
}
 
 
Function Uninstall
{
    Param(
        [Parameter(Mandatory=$false)]
        [string]$inp
        )
 
    write-host "`n`n@@ Called Uninstall" -fore Magenta
 
 
    $paths = @(
        'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
        'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
        ) | where-object { test-path $_ }
 
    $items = @(dir $paths | % { gp $_.PsPath } | ? { $_.DisplayName -imatch $inp })
 
    foreach ($item in $items)
    {
        $item | gm | ? { $_.Name -imatch 'Path_' } | % {
         
            write "Delete : $($item.($_.Name))"
            if (!$debug) { remove-item $item.($_.Name) -recurse -force -ea SilentlyContinue }
        }
        write "Delete : $($item.PSPath)"
        if (!$debug) { remove-item $item.PSPath -force -ea SilentlyContinue }
    }
}
 
 
#
#
 
 
Clear
KillApps
Uninstall Dropbox
Unpack
Setup