title: Regular expression to delete the whole line include the end of line
body: <p>I want to delete all the commented lines in a powershell script using another powershell script. I expected this to be very easy, but apparently it is not. Here are the things I tried, and obviously didn't work:</p>
  
  <pre><code>(Get-Content commented.ps1) -replace '^#.*$', '' | Set-Content uncommented.ps1
  (Get-Content commented.ps1) -replace '#.*$', '' | Set-Content uncommented.ps1
  </code></pre>
  
  <p>These worked, but the end of line is still there, so now I have some empty lines instead of the comments, which is not what I want.</p>
  
  <pre><code>(Get-Content commented.ps1) -replace '#.*\r\n', '' | Set-Content uncommented.ps1
  (Get-Content commented.ps1) -replace '^#.*\r\n$', '' | Set-Content uncommented.ps1
  (Get-Content commented.ps1) -replace '#.*\r\n$', '' | Set-Content uncommented.ps1
  </code></pre>
  
  <p>I also tried to write just <code>\n</code>, even though I'm sure my file is CRLF. And I also tried to put <code>\n</code> or <code>\r\n</code> at the beginning. These did not work at all, but they also didn't error. </p>
  
  <p>Test files:</p>
  
  <p>commented.ps1 :</p>
  
  <pre><code>#This is a comment
  $var = 'this is a variable'
  # This is another comment
  $var2 = 'this is another variable'
  </code></pre>
  
  <p>expected uncommented.ps1 : </p>
  
  <pre><code>$var = 'this is a variable'
  $var2 = 'this is another variable'
  </code></pre>
  
  <p>I simply don't get it why <code>\r\n</code> does not match end of line. Any help is highly appreciated. I guess the question is:</p>
  
  <p>How to match the end of the line successfully in powershell using <code>Get-Content -replace</code>?</p>