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?
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.
$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/…
Load the assembly first:
add-type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(10,0)
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.
GeneratePassword
requires two arguments.