How to Delete Old IIS Logs with Scheduled Task

IIS can rotate log files.  I just wish it could also delete log files older than x number of days.

Since IIS 7.5 does not do it, I would have to write a quick powershell script for it.

To begin, the following will list all files within the folder/subfolders (i.e. the whole tree recursively) that is older than 30 days and match the file name pattern “u_ex*.log”.

   1: $srcPath = "C:\inetpub\logs\LogFiles\"

   2: $filePattern = "u_ex*.log"

   3: $cutoffDays = 30


   5: # output the file names to delete

   6: Get-ChildItem $srcPath -Include $filePattern -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(0-$cutoffDays)} | out-string


Once you are comfortable, you can replace “out-string” with “Remove-Item”.  It will delete those old files.

   1: $srcPath = "C:\inetpub\logs\LogFiles\"

   2: $filePattern = "u_ex*.log"

   3: $cutoffDays = 30


   5: # delete the files

   6: Get-ChildItem $srcPath -Include $filePattern -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(0-$cutoffDays)} | Remove-Item


Say you also want to keep a log of the files you are deleting:

   1: $srcPath = "C:\inetpub\logs\LogFiles\"

   2: $filePattern = "u_ex*.log"

   3: $cutoffDays = 30

   4: $logFolder = "C:\temp\"

   5: $logPath = $logFolder + (Get-Date).tostring("yyyyMMdd") + "_cleanuplog.txt"


   7: # log file names to delete to a log file

   8: Get-ChildItem $srcPath -Include $filePattern -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(0-$cutoffDays)} | out-file $logPath -Append


  10: # delete the files

  11: Get-ChildItem $srcPath -Include $filePattern -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(0-$cutoffDays)} | Remove-Item


Special thanks to Nathan Hartley’s powershell answer.


MVC ASP.NET Identity OWIN Authentication Not Working for IE


We have a MVC App which uses ASP.NET Identity, which uses OWIN.

We test the login authentication using 3 browsers:

  • Google Chrome
  • Firefox
  • IE 11

It works fine for all browsers in our local workstations using IIS Express and localhost.


However, when we deployed it to a test environment, IE fails to authentication while Chrome and Firefox succeeded.

The test environment is using IIS 8.


It turns out that IE does not like domain name with underscore and we were using a URL like “”.  IE fails to cookies.


We use a different subdomain name without underscore.