Skip to content

Instantly share code, notes, and snippets.

@jdforsythe
Created February 16, 2016 20:28
Show Gist options
  • Save jdforsythe/d2c666037bcd61547cc0 to your computer and use it in GitHub Desktop.
Save jdforsythe/d2c666037bcd61547cc0 to your computer and use it in GitHub Desktop.
Execute Powershell Cmdlet in C#
using System.Management.Automation;
namespace ExecutePowershellCmdlet {
class Program {
using (PowerShell pshell = PowerShell.Create()) {
DateTime yesterday = DateTime.Today.AddDays(-1);
string date_string = "\"" + yesterday.ToString("MM"_ + "/" + yesterday.ToString("dd") + "/" + yesterday.ToString("yyyy") + "\""; // "MM/dd/yyyy"
string name_regex = "'^[0-9]{4,4}\\.dw*$'";
pshell.AddScript("dir \"k:\\jobs\\\" -Directory | Where-Object { $_.LastWriteTime -ge " + date_string + " -and $_.Name -match " + name_reges + " } | Copy-Item -Destination c:\\copy_to -recurse");
pshell.Invoke();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment