Showing posts with label Remove-Item. Show all posts
Showing posts with label Remove-Item. Show all posts

Thursday, March 1, 2012

Clean Disk Space using PowerShell.


Hi,

I wrote a simple PowerShell script which helps us to clean some of the disk space for us. This script wrote primarily for LENOVO laptops but can tweak it as per your requirements.

This Script does the following tasks:

  • ·         Clean users TEMP folder
  • ·         Empty Recycle Bin
  • ·         Empty SWTOOL Folder {mainly in Lenovo laptops}
  • ·         Empty Windows TEMP Folder
  • ·         Run Windows  Disk Cleanup Tool.


I hope that you like this tiny script J


Thanks
Aman Dhally

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

Friday, January 20, 2012

How to use –Exclude switch in Remove-Item cmdlet in PowerShell


Hi,
today i was trying to remove some junk folders and files in a  specific folder. But i don’t want to remove all of file and folder , i want to keep few folders. Then i think that i should use the –Exclude switch with Remove-Item Cmdlet.
I tried to use –Exclude switch but somehow i failed to get it working because i don't know the exact patter used by –Exclude switch. After few minutes of testing i get it done and i thought i should share this tip with you.
  1. $Lenovo = "D:\P-Temp\Lenovo\*"
  2. Remove-Item -Recurse  -Path $Lenovo -Exclude system,temp,updates.ser,"*.xml"   -VerboseForce
In $Lenovo variable i specified the folder path in which I want to remove the items
I want to Exclude folder name “System”,”temp” and file name “updates.ser” and all .XML Files
in -Exclude switch give the folder name which you don’t want to delete,  no need to put the in a double quotes “” in folder name . You can provide multiple folder name separated by comma.
in pattern matching make sure you put the wild cards in double quotes “”.

Lets Test it !!
see we have System,Temp, few XML and one Updates.Ser file which we exclude to deleted. lets run the script now and see which file is getting deleted.
20-01-2012 12-48-32
after running the script .
All folders are stay intact which we exclude in the Remove-Item cmdlet.
20-01-2012 12-52-56
I hope that i helps someone .
thanks
aman dhally