getting return code from Invoke-RestMethod

Is there a way to get a return code (i.e., 200, 404, etc.) from Invoke-RestMethod? I am getting the response string but I would like the numeric return code from the REST call as well. Thanks.

Please use Invoke-WebRequest…

With Invoke-WebRequest, this can be done. The details are that if you assign the return value from the cmdlet to $Response, the return code of the web call is $Response.StatusCode while the main content is $Response.Content. Thank you.

In PowerShell 6, Invoke-RestMethod has a “ReponseHeadersVariable” parameter for this. Unfortunately, it isn’t available in 5.1 and earlier, so the workaround as already mentioned is to use Invoke-WebRequest.

Had no idea about the ReponseHeadersVariable! Great tip John!