I've trying to get the password hash for a SQL authentication login via powershell.

The code I'm executing is something like this

Invoke-Sqlcmd -Query "SELECT LOGINPROPERTY('$LoginName', 'PASSWORDHASH') as PasswordHash"  -ServerInstance $SourceInstance

This works in that the query executes fine but the output I get is


{2, 0, 87, 116...}

I'm guessing (probably incorrectly) that this is due to it returning a varbinary, has anyone experienced this and/or fixed it before?

On a side note I'm pretty sure that a script I have which configures tempdb gets similar output. I'd like to understand not just how to fix it but why it happens too :)

Cheers Mat

1 Answer 1


For SQL 2005+

Invoke-Sqlcmd -Query "SELECT sys.fn_varbintohexstr(CONVERT(VARBINARY(MAX), LOGINPROPERTY('$LoginName', 'PASSWORDHASH'))) as PasswordHash"  -ServerInstance $SourceInstance

You can't use this for everything as it has length limitations, but for this purpose it will be fine.

  • Thanks Cody, that worked well and now outputs correctly.
    – Mat
    Commented Jun 30, 2016 at 12:31

Your Answer

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

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