14

Why do I get Type Not Found when trying to use this class/method in PowerShell?

 [System.Web.Security.Membership]::GeneratePassword()

Do I need to install a module or something to make use of it?

7
  • Haha i should've known. Forgot about having to add it first. Long day! Thanks.
    – Rakha
    Nov 14, 2018 at 19:56
  • What version of .NET and PowerShell are using? I see this on 5.1 in both Win7 and Win10. Additionally, GeneratePassword requires two arguments. Nov 14, 2018 at 19:56
  • 5.1.17134.228 , .net 4.7.03056 i think
    – Rakha
    Nov 14, 2018 at 19:57
  • Did you remove the Add-type comment answer? Please put it back since it can be useful.
    – Rakha
    Nov 14, 2018 at 19:58
  • 2
    Strange, i really had to do add-type -AssemblyName System.Web
    – Rakha
    Nov 14, 2018 at 20:14

3 Answers 3

26

For those landing here who are using PowerShell Core, as per this link:

https://github.com/PowerShell/PowerShell/issues/5352

.NET Core does not support System.Web.dll

As such [System.Web.Security.Membership]::GeneratePassword() will result in the same Type Not Found error in PowerShell Core even with the add-type fix above.

You'll have to roll your own password generator.

4
  • 6
    Sad that there is no inbuilt alternative. I'm using $pass = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..12] -join '' per [this blog][1] [1]: undocumented-features.com/2016/09/20/…
    – Negatar
    Sep 24, 2020 at 22:20
  • Here's a nice blog post on how to do it: dev.to/onlyann/user-password-generation-in-powershell-core-1g91
    – ebu_sho
    Nov 22, 2021 at 6:29
  • 1
    This answer is not entirely accurate. Using PowerShell 5.1 will work, but using 6+ will not. Oct 21, 2022 at 14:44
  • 1
    @RashadRivera Well, yes, but this particular answer relates specifically to PowerShell Core and is accurate for that version. Versions 1.0 through 5.1 of PowerShell are known as Windows PowerShell. Version 6.0 is known as PowerShell Core. Versions 7+ are known as PowerShell.
    – DefChip
    Oct 25, 2022 at 13:59
13

Load the assembly first:

add-type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(10,0)
1

You do not need to roll your own. You only need to understand how PowerShell is behaving in version 6+.

I'm running PowerShell 7 and when I try to use the techniques described here, I get the same Membership type is not found error.

When using PowerShell 5.1, I confirmed that before using the Add-Type -AssemblyName System.Web, the Membership type was not found. After using it, it works.

Going forward, you will need to find a different solution for PowerShell versions 6+. But so long as you have 5, you should be able to add the System.Web assembly to access Membership class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.