Powershell: 7-Zip installieren und Dateizuordnungen setzen

PowerShell Icon (c)

Das Script beginnt mit einer Funktion, die – falls nicht gegeben – administrative Rechte einfordert, um die Software korrekt installieren zu können und ebenso die Dateizuordnungen einzurichten.

Abzuspeichern etwa als „7zip.ps1“ an beliebigem Ort.

Zur Ausführung genügt ein Rechtsklick auf die Datei mit anschließender Auswahl „Mit PowerShell ausühren“ bzw. „Run with PowerShell“:

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Host "Running elevated..."
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    Break
}

function register_filetypes() {
    cmd /c "ftype 7-Zip.001=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.7z=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.arj=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.bz2=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.bzip2=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.cab=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.cpio=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.deb=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.dmg=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.fat=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.gz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.gzip=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.hfs=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.iso=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.lha=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.lzh=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.lzma=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.ntfs=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.rar=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.rpm=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.squashfs=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.swm=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.tar=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.taz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.tbz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.tbz2=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.tgz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.tpz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.txz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.vhd=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.wim=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.xar=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.xz=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.z=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "ftype 7-Zip.zip=""C:\Program Files\7-Zip\7zFM.exe"" ""%1"""
    cmd /c "assoc .001=7-Zip.001"
    cmd /c "assoc .7z=7-Zip.7z"
    cmd /c "assoc .arj=7-Zip.arj"
    cmd /c "assoc .bz2=7-Zip.bz2"
    cmd /c "assoc .bzip2=7-Zip.bzip2"
    cmd /c "assoc .cab=7-Zip.cab"
    cmd /c "assoc .cpio=7-Zip.cpio"
    cmd /c "assoc .deb=7-Zip.deb"
    cmd /c "assoc .dmg=7-Zip.dmg"
    cmd /c "assoc .fat=7-Zip.fat"
    cmd /c "assoc .gz=7-Zip.gz"
    cmd /c "assoc .gzip=7-Zip.gzip"
    cmd /c "assoc .hfs=7-Zip.hfs"
    cmd /c "assoc .iso=7-Zip.iso"
    cmd /c "assoc .lha=7-Zip.lha"
    cmd /c "assoc .lzh=7-Zip.lzh"
    cmd /c "assoc .lzma=7-Zip.lzma"
    cmd /c "assoc .ntfs=7-Zip.ntfs"
    cmd /c "assoc .rar=7-Zip.rar"
    cmd /c "assoc .rpm=7-Zip.rpm"
    cmd /c "assoc .squashfs=7-Zip.squashfs"
    cmd /c "assoc .swm=7-Zip.swm"
    cmd /c "assoc .tar=7-Zip.tar"
    cmd /c "assoc .taz=7-Zip.taz"
    cmd /c "assoc .tbz=7-Zip.tbz"
    cmd /c "assoc .tbz2=7-Zip.tbz2"
    cmd /c "assoc .tgz=7-Zip.tgz"
    cmd /c "assoc .tpz=7-Zip.tpz"
    cmd /c "assoc .txz=7-Zip.txz"
    cmd /c "assoc .vhd=7-Zip.vhd"
    cmd /c "assoc .wim=7-Zip.wim"
    cmd /c "assoc .xar=7-Zip.xar"
    cmd /c "assoc .xz=7-Zip.xz"
    cmd /c "assoc .z=7-Zip.z"
    cmd /c "assoc .zip=7-Zip.zip"
}

Write-Host "Installing 7-Zip ..."
$instPath = "$env:ProgramFiles\7-Zip\7z.exe"
if (!(Test-Path $instPath))
{
    Write-Host "Determining download URL ..."
    $web = New-Object System.Net.WebClient
    $page = $web.DownloadString("http://www.7-zip.org/download.html")
 
    $64bit = ''
 
    if ($env:PROCESSOR_ARCHITECTURE -match '64')
    {
        $64bit = 'x64'
    }
 
    $pattern = "(http://.*?${64bit}\.msi)"
    $url = $page | Select-String -Pattern $pattern | Select-Object -ExpandProperty Matches -First 1 | foreach { $_.Value }
 
    $file = "$env:TEMP\7z.msi"
    if (Test-Path $file)
    {    
        rm $file | Out-Null
    }
    
    Write-Host "Downloading $url -> $file"
 
    $web.DownloadFile($url, $file)
 
    Write-Host "Installing..."
 
    $cmd = "$file /passive"
 
    Invoke-Expression $cmd | Out-Null
 
    while (!(Test-Path $instPath))
    {
        Start-Sleep -Seconds 10
    }
    Write-Host "Associating file types..."
    register_filetypes | Out-Null
    Write-Host "Done!"
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
 
}
else
{
    Write-Host "7-Zip already installed."
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.