title: How to run a powershell command in c#
body: <p>So I am attempting to change the creation date of a text file in C#. The user will type in thr creation date of the file, it will then change the text files creation date to what the user inputted. Trouble is it keeps adding a ' for some reason resulting in an error message, it is calling on Powershell to accomplish this:</p>
  <pre><code>    public partial class Form6 : Form
      {
          public Form6()
          {
              InitializeComponent();
          }
  
          private void textBox1_TextChanged(object sender, EventArgs e)
          {
              
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              string dir = textBox1.Text;
  
              PowerShell ps = PowerShell.Create();
  
  
  
            ps.AddCommand(&quot;Get-ChildItem c:\\encryptedmessagehere.txt | % {$_.CreationTime = '&quot;+ dir + &quot;'}&quot;);
  
              ps.Invoke();
          }
      }
  }
  </code></pre>
  <p>Trouble is, after the &quot;'}&quot;);, it automatically adds another ', incorrecting the powershell command to change the date. Is there a way to stop it from adding the ' at the end?</p>
  <p>The returned error is:</p>
  <p>System.Management.Automation.CommandNotFoundException
  HResult=0x80131501
  Message=The term 'Get - ChildItem  C:\encryptedmessagehere.txt | % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.</p>
  <p>Thank you.</p>