title: Extract database users with passwords
body: <p>I want to script out the database users in my contained database using sql login. I am using the below query but it is only creating a database user and not a sql login with password. Is it possible to script out the password in a encrypted output so I can restore it properly? </p>
  
  <pre><code>SELECT '-- [-- DB USERS --] --' AS [-- SQL STATEMENTS --], 3 AS [-- RESULT ORDER HOLDER --] 
  UNION SELECT  'IF NOT EXISTS (SELECT [name] FROM sys.database_principals WHERE [name] = ' + SPACE(1) + '''' + [name] + '''' + ') 
  BEGIN CREATE USER ' + SPACE(1) + QUOTENAME([name]) + ' FOR LOGIN ' + QUOTENAME([name]) + ' WITH DEFAULT_SCHEMA = ' + QUOTENAME([default_schema_name]) + SPACE(1) + 'END; ' AS [-- SQL STATEMENTS --], 4 AS [-- RESULT ORDER HOLDER --] FROM sys.database_principals AS rm
  WHERE [type] IN ('U', 'S', 'G') -- windows users, sql users, windows groups
  </code></pre>
  
  <p>result</p>
  
  <pre><code>IF NOT EXISTS (SELECT [name] FROM sys.database_principals WHERE [name] ='DJlogin') BEGIN CREATE USER  [DJlogin] FOR LOGIN [DJlogin] WITH DEFAULT_SCHEMA = [dbo] END;
  </code></pre>