How to run a powershell command in c#

Solution 1:

First, you need to use Powersheel.AddScript method instead of Powersheel.AddCommand method.

Second, you can try the following Powershell code to change the creation time of the file.

(Get-ChildItem d:\Test\new.txt).CreationTime =  '2020/09/23'

Finally, you can try the following c# code example to call powershell code in c#.

private void button1_Click(object sender, EventArgs e)
        {
            string datetime= textBox1.Text;
            PowerShell ps = PowerShell.Create();
            string script = string.Format("(Get-ChildItem d:\\New.txt).CreationTime = '{0}'", datetime);
            ps.AddScript(script);
            ps.Invoke();
            MessageBox.Show("Test");
        }