<# 
setup.ps1
 
Uninstalls 7-zip and installs current version
 
http://msdn.microsoft.com/en-us/library/aa394373%28v=VS.85%29.aspx
Win32_Processor.AddressWidth provides the Operating System's word-width
Win32_Processor.DataWidth provides the Processor's word-width
 
Place x86 and x64 installers in .\setup subdirectory.
 
OS: XP, W7, W8
Arch: x86, x64
 
Adam Sailer
2013.08.05
#>
 
 
## Globals
$invoke = split-path -path $myinvocation.mycommand.path -parent
$os = gwmi Win32_OperatingSystem
$proc = gwmi Win32_Processor
$debug = $false
 
 
Function KillApps
{
    write-host "`n`n@@ Called KillApps" -fore Magenta
    get-process -name *7z* | stop-process -force
}
 
 
Function Uninstall
{
    Param([string]$inp)
 
    write-host "`n`n@@ Called Uninstall" -fore Magenta
        
    if (!$inp) { Return }
 
    $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)
    {
        if ($debug) { write $item }
        $uString = $item.UninstallString
                         
        if ($uString)
        {
            $app = $uString
            $options = '/S'
             
            if ($uString -imatch 'msiexec')
            {
                $array = $uString.Split(' {}')
                $app = $array[0]
                $options = "/qn /x {$($array[2])}"
            }                                             
             
            write "Uninstalling : $($item.DisplayName)"
            if (!$debug)
            {
                $process = (Start-Process -FilePath $app -ArgumentList $options -Wait -Passthru)
                if ($process.ExitCode) { write $process.ExitCode; exit $process.ExitCode }
            }
        }
    }
}
 
 
Function Setup
{
    write-host "`n`n@@ Called Setup" -fore Magenta
         
    $identifier = 'x86'
    if (($proc.AddressWidth -eq 64) -and ($proc.DataWidth -eq 64))
    { $identifier = 'x64' }
     
    $apps = @(dir -recurse $invoke\setup -include *$identifier.msi)
 
    foreach ($app in $apps)
    {
        write "Installing : $($app.Name)"
        $options = "/qn /i `"$app`""
        
        if (!$debug)
        {             
            $process = (Start-Process -FilePath msiexec.exe -ArgumentList $options -Wait -Passthru)
            if ($process.ExitCode) { write $process.ExitCode; exit $process.ExitCode }
        }
    }
}
 
 
#
#
 
 
Clear
KillApps
Uninstall 7-Zip
Setup