title: How to get Database Name from Connectionstring in PowerShell
body: <p>I'm trying to get the Database name from a connection string in PowerShell.</p>
  
  <pre><code>"Server=server\instance;uid=User;pwd=Hello;Database=SomeName;"
  </code></pre>
  
  <p>I can think of two ways to do that, either to search for the string <code>Database</code>, up until the first <code>;</code> after that split the string on <code>=</code> and select the Databasename - but I don't really know how to do that.</p>
  
  <p>The second way could be with the <code>DBConnectionStringBuilder</code> like this:</p>
  
  <pre><code>$sb = New-Object System.Data.Common.DbConnectionStringBuilder
  $sb.set_ConnectionString($cstring)
  [string]$Database = ($sb | ? {$_.Keys -eq 'Database'}).value
  </code></pre>
  
  <p>but with this way, no matter how hard i try to filter the Databasename, it won't give me the databasename returned.</p>
  
  <p>Question: What's the best way to get my Databasename from the connection string?</p>