Share via


Using .NET Classes in PowerShell

Error


New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Net.WebRequest. At line:1 char:7 + $wr = New-Object -TypeName System.Net.WebRequest + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand


The error message is clear, now it's time to look into the Microsoft documentation. Reference Link

Remarks

Use the Create method to initialize new WebRequest instances.

Do not use the constructor.

Example C#

WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

PowerShell Code

$wr = [System.Net.WebRequest]::Create('http://technet.microsoft.com')
$wr | GM
$wr = [System.Net.WebRequest]::Create('http://technet.microsoft.com')
$wr.ReadWriteTimeout