Amrit Swaroop Sinha
Amrit Swaroop Sinha

Reputation: 3

New-CryptographyKey module Powershell

I have been working on a script and I needed to use Encryption/Decryption. Basically, encrypt a text file and then add the decryption code in my script and then let the script do its work by taking the encrypted file and decrypting it. After googling through stuff, I came across this post. By far it seemed the most simple implementation for my work. However, I am unable to import this module in my PS window. When I write:

Import-Module New-CryptographyKey

I get the error:

Import-Module Cannot find path 'C:\WINDOWS\system32\New-CryptographyKey' because it does not exist.

I understand that this is some path issue but I have set the path in the environment.

Any suggestions will be helpful.

Upvotes: 0

Views: 2015

Answers (2)

Amrit Swaroop Sinha
Amrit Swaroop Sinha

Reputation: 3

So what I was missing was to importing the module as stated by TheIncorrigible1. After that, I was also missed adding the Assembly as follows in the Script.:

Add-Type -Assembly System.Security

Add-Type -AssemblyName System.Windows.Forms

How this worked is that I used the Technet link and understood what he was doing and used the assemblies that he imported in my script and extracted the Encrypting and the Decrypting statements he used. This seemed to work for me.

This happened because I was unable to Import the New-CryptographyKey because I was not specifying the path. So for anyone else, better to import the module with its path when you are facing this issue.

Thanks Incorrigible1 for letting me know of this, however I made this work in a noob way but the correct way to import it was by giving the correct path as

Import-Module -Name 'C:\path\to\FileCryptography.psm1'

Upvotes: 0

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

Your problem is how you're importing the module. Because the technet link you have in your question is directly to a .psm1 file, you need to fully-path that in your import command (as it does not have a proper module manifest):

Import-Module -Name 'C:\path\to\FileCryptography.psm1'

With this, it should work.

The alternative is you generate a module manifest, learn how module loading works and have the folder/files in the right location/named correctly, and then it can be auto-loaded on v3+, but that's a little outside the scope of this question.

Upvotes: 1

Related Questions