# Encrypting and decrypting PGP using PowerShell

Some time ago, I decided that having an easy-to-use PGP PowerShell module is a way to kill my boredom. Four months have passed, and I decided to share it with the world, as it may be helpful to some of you. Today I would like to introduce you to PSPGP – PowerShell module that provides PGP functionality in PowerShell.

blog

12 Sep 2021 pl

![Encrypting and decrypting PGP using PowerShell](/wp-content/uploads/2021/08/img_612a8a9916eb2.png)

[Zobacz obrazy (2)](#pf-content-image-1-0)

Some time ago, I decided that having an easy-to-use **PGP PowerShell** module is a way to kill my boredom. Four months have passed, and I decided to share it with the world, as it may be helpful to some of you. Today I would like to introduce you to [**PSPGP**](https://github.com/EvotecIT/PSPGP) – PowerShell module that provides **PGP functionality** in **PowerShell**.

### 💡 PSPGP – Commands available

[PSPGP](https://github.com/EvotecIT/PSPGP) is a fairly small PowerShell module that has only four commands at the moment of writing. Those are:

- 📝 **New-PGPKey** – cmdlet to create public/private **PGP** keys
- 📝 **Protect-PGP** – cmdlet to encrypt folder/files
- 📝 **Unprotect-PGP** – decrypt **PGP** encrypted folder/file
- 📝 **Test-PGP** – verify signed **PGP** file

While the module itself is tiny – that's what makes it very powerful and easy to use.

### 💡 PSPGP – Create Private/Public PGP keys

**PGP** works based on **public** and **private** keys. Those can be generated using the **New-PGPKey** command, as shown below

```powershell
New-PGPKey -FilePathPublic $PSScriptRoot\Keys\PublicPGP.asc -FilePathPrivate $PSScriptRoot\Keys\PrivatePGP.asc -UserName 'przemyslaw.klys' -Password 'ZielonaMila9!'
```

### 💡 PSPGP – Encrypt files & folders with PGP

Once you have private and public keys generated, you're ready to encrypt the folder using someone's public key and send it over.

```powershell
Protect-PGP -FilePathPublic $PSScriptRoot\Keys\PublicPGP.asc -FolderPath $PSScriptRoot\Test -OutputFolderPath $PSScriptRoot\Encoded
```

### 💡 PSPGP – Decrypt PGP files

Similarly, if someone sends you content **encrypted with a public key**, you can now **decrypt it with your own private key and password**.

```powershell
Unprotect-PGP -FilePathPrivate $PSScriptRoot\Keys\PrivatePGP.asc -Password 'ZielonaMila9!' -FolderPath $PSScriptRoot\Encoded -OutputFolderPath $PSScriptRoot\Decoded
```

Of course, PGP also supports a way to encrypt/decrypt strings.

```powershell
$ProtectedString = Protect-PGP -FilePathPublic $PSScriptRoot\Keys\PublicPGP.asc -String "This is string to encrypt"
Unprotect-PGP -FilePathPrivate $PSScriptRoot\Keys\PrivatePGP.asc -Password 'ZielonaMila9!' -String $ProtectedString
```

[!\[PGP Decrypt\](/wp-content/uploads/2021/08/img_612a8a9916eb2.png)](/wp-content/uploads/2021/08/img_612a8a9916eb2.png)

### 💡 PSPGP – Verify signature of PGP files

Finally, one can always verify signature by using **Test-PG**P command

```powershell
$ProtectedString = Protect-PGP -FilePathPublic $PSScriptRoot\Keys\PublicPGP.asc -String "This is string to encrypt"
Test-PGP -FilePathPublic $PSScriptRoot\Keys\PublicPGP.asc -String $ProtectedString
Test-PGP -FilePathPublic $PSScriptRoot\Keys\PublicPGP.asc -FolderPath $PSScriptRoot\Encoded
```

[!\[PowerShell console encrypting and testing PGP files with PSPGP\](/wp-content/uploads/2021/08/img_612a8b6fb6400.png)](/wp-content/uploads/2021/08/img_612a8b6fb6400.png)

### 💡 PSPGP – Installing PGP PowerShell Module

To run it, just install it from **PowerShellGallery,** and you're good. If you are not an administrator, you can use this module within the scope of the current user.

```powershell
Install-Module PSPGP -Force -Scope CurrentUser
```

If, however, you would like to make sure the module is available machine-wide, you can do this without providing scope.

```powershell
Install-Module PSPGP -Force
```

All source codes are available on [GitHub](https://github.com/EvotecIT/Transferetto). If you have an issue, feature request, problem, please use GitHub as a way to reach for support. As I have limited time, reaching out via email doesn't bring many results. As with many of my other PowerShell modules, it's always a work in progress, and not everything is 100% finished. Please keep in mind this module works **cross-platform** on Windows/Linux and macOS. **For PowerShell 5.1, it requires .NET Framework 4.7.2 at a minimum to work**.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:A3A32BECAA982CA75354E11ABBC51D6C620A95ABEAF70FF6B35969484303D11A -->
