title: Encrypt zip archive using PowerShell script
body: <p>I have a PowerShell script function that creates a text file and then creates a zip file and encrypts it. The code that I have written is as below.</p>
  
  <pre><code>function Zip-File([String]$Name, [String]$getRandomPass, [String]$EncryptionKey) {
      try {
          $7ZipPath = "C:\Program Files\7-Zip\7z.exe";
          $Filepath = "C:\Scripts\$Name.txt";
          $ZipPath = "C:\Scripts\$Name.zip";
  
          Set-Content -Path $Filepath -Value $getRandomPass -Force;
  
          $arguments = "a -tzip ""$ZipPath"" ""$Filepath"" -mx9 -p$EncryptionKey";
          $windowstyle = "Normal";
          $p = Start-Process $7ZipPath -ArgumentList $arguments -Wait -Passthru -WindowStyle $windowstyle;
      } catch {
          Write-Host "Error is: $($_.Exception.GetType().FullName)"
          Write-Host "Error is: $($_.Exception.Message)"
      } finally {}
  }
  </code></pre>
  
  <p>This code creates the zip folder <code>$Name.zip</code> with the file <code>$Name.txt</code>. However, only the file <code>$Name.txt</code> is encrypted with the encryption key. The zip archive <code>$Name.zip</code> is not encrypted.</p>
  
  <p>I am not sure how to encrypt the <code>$Name.zip</code> zip archive. Could someone point out how to encrypt the <code>$Name.zip</code> zip archive?</p>