powershellopenfiledialog

Powershell Script Function Select and SaveAs


I have a script what should do the following: Open a File with a Dialog Box and save the selected File as a compressed Archive and automaticly create the Output on a Destination (say C:\Temp)

The Dialog Box I have made. However I cannot for the life of me figure a way out to save the selected File as a compressed Archive=Zip, optimaly with a password.

This is what I have created so far:

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = 'Filetype (*.mp4)|*.mp4'
}
$null = $FileBrowser.ShowDialog()

Hopefully someone has an Idea.

Thanks in advance

Tod

Select a file in a Dislog Box and Output a compressed archive = Zip file


Solution

  • To get this script to work you have to install the module 7Zip4Powershell, it should work without having 7zip installed, thats the problem i was having i installed 7zip called it and then it worked.

    Add-Type -AssemblyName System.Windows.Forms
    
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        InitialDirectory = [Environment]::GetFolderPath('Desktop')
        Filter = 'Filetype (*.mp4)|*.mp4'
    }
    
    if ($FileBrowser.ShowDialog() -eq 'OK') {
        $selectedFile = $FileBrowser.FileName
        $output = "C:\Temp\Exit"
        
        $password = Read-Host "Enter password"
    
        $sevenZipPath = "C:\Program Files\7-Zip\7z.exe"
    
        $arguments = "a -tzip `"$output`" `"$selectedFile`" -p$password"
    
        Start-Process -FilePath $sevenZipPath -ArgumentList $arguments -Wait
    
        Write-Host "Zip Created"
    }