Tuesday, February 14, 2012

Delete Offline OST files from login users account using PowerShell.


Hi,

Yesterday I was surfing the TECHNET Script repository and see that one user is requested for a script which can delete offline email file {ost} from login users.

This seems simple to me and I wrote a 3line script which can accomplish this task.


1.  $OstPath = $Env:LocalAppData + "\Microsoft" + "\Outlook"
2.  $ost = get-ChildItem $OstPath | where { $_.Extension -eq ".ost"} 
3.  $ost | remove-Item -WhatIf ## remove -whatif to remove the files 

1.    In windows7 and with outlook 2010 installed the OST file are created in “C:\Users\USERNAME\AppData\Local\Microsoft\Outlook” folder. The $Env:LocalAppData windows environment variable contain the path of “C:\Users\aman.dhally\AppData\Local” and we added “\Microsoft” and “\Outlook” to make the complete path to the outlook folder.
2.    Secondly we are getting all files whose extension is equal .OST and saving the result in to the $ost variable
3.    Thirdly we are parsing the output of $ost to remove-Item to remove all files.

Screenshots:

Below is the path of my OST files.

14-02-2012 12-08-29 

See the default value of  $Env:LocalAppData data and the output of $OstPath when we added "Microsoft" and "Outlook" in to it.

14-02-2012 12-11-07
$ost = get-ChildItem $OstPath | where { $_.Extension -eq ".ost"}  , and just see the output of $ost you can see that is is showing the name of all OST which we have in out outlook folder.

14-02-2012 12-13-31
$ost | remove-Item -WhatIf
and this will remove the OST files :)
14-02-2012 12-15-16


Simple isn't:)


Note: I tested this script on Windows7 with Outlook 2010 installed.

Thanks
Aman Dhally

4 comments:

  1. Aman, Would there be a way to do for all users on a computer?

    ReplyDelete
    Replies
    1. Hi Mac,
      How to want to provide the usernames? because if we got all users profile name and use this with foreach then i think can be done

      $Ost = "C:\users" + "\username" + "\Appdata\Local\Outlook"

      Delete
  2. ost recovery easily opens, recovers, moves your messages, contacts, notes, meetings, reminders, tasks and all other information. Software has easy to use, intuitive interface even for novices. Application starts under any Windows OS starting with Windows 98.

    ReplyDelete
  3. Aman, This is a great little script to remove the files. However remember that if Outlook's data file settings are not set to "disable offline use" the OST files will be recreated every time the user opens outlook.

    ReplyDelete