How to get StatusCode from request in PowerShell How to get StatusCode from request in PowerShell powershell powershell

How to get StatusCode from request in PowerShell


did you try pipe'ing with select-object ?

Invoke-WebRequest -Uri apiEndpoint -UseBasicParsing | Select-Object StatusCode

output

StatusCode ----------   200

or just to get status code

Invoke-WebRequest -Uri apiEndpoint -UseBasicParsing | Select-Object -Expand StatusCode

output

200

to handle unsuccessful cases,on PowerShell v#7 + include -SkipHttpErrorCheck parameteroryou may need to make use of $Error array to get most recent error and access properties accordingly.


curl -Uri 'google.com' | select-object StatusCode