# 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 en

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

[View images (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 session using Test-PGP to verify an encrypted string and three .pgp files with a True status for each result.\](/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**.

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [PowerBGInfo 2.x: support-ready Windows backgrounds from PowerShell](/blog/powerbginfo-support-ready-windows-backgrounds/)
- [Track Ubiquiti Unifi Stock, and get notified of changes](/blog/simplify-notifications-about-ubiquiti-unifi-stock/)
- [PowerBGInfo – PowerShell alternative to Sysinternals BGInfo](/blog/powerbginfo-powershell-alternative-to-sysinternals-bginfo/)

<!-- PowerForge markdown artifact sha256:1D7A00737C9FAD11A022419DF732A592CEF20A540E8AE7B80420E6489C3E2B9D -->
