title: How to use a variable as part of a regular expression in PowerShell
body: <p>I want to <a href="https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-String" rel="nofollow noreferrer">Select-String</a> parts of a file path starting at a string value that is contained in a variable. Let me explain this in an abstracted example.</p>
  <p>Let's assume this path: /docs/reports/test reports/document1.docx</p>
  <p>Using a regular expression I can get the required string like so:
  <code>'^.*(?=\/test\s)'</code></p>
  <p><a href="https://regex101.com/r/6mBhLX/5" rel="nofollow noreferrer">https://regex101.com/r/6mBhLX/5</a></p>
  <p>The resulting string is '/test reports/document1.docx'.</p>
  <p>Now, for this to work I have to use the literal string 'test'. However, I would like to know how to use a variable that contains 'test', e.g. $myString.</p>
  <p>I already looked at <em><a href="https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression">How do you use a variable in a regular expression?</a></em>, but I couldn't figure out how to adapt this for PowerShell.</p>