Windows Server TechCenter >
Windows Server Forums
>
Windows PowerShell
>
Deleting Temporary Internet Files
Deleting Temporary Internet Files
- I'm working on a script to delete temporary internet files from all profiles on a given list of servers (namely our terminal servers that keep getting filled up with garbage). I've got it working but I get lots of errors during the deletions because the temp files have file names larger than 260 characters.
Here's what I have:
# Declare the servers
# ===================
#$tservers = [real server list]
# @ TESTING BLOCK
# @
$tservers = [test machines]
# @
# @
#Display a little info on how many profiles you find
# ==================================================
foreach ($server in $tservers) {
write ''
write "Server: $Server"
$profiles = get-childitem "\\$server\C$\Documents and Settings" -force
write "Found $($profiles.count) profiles on $server."
# Delete the temporary internet files for each
# ============================================
foreach ($userprofile in $profiles) {
write "Deleting $server\$userprofile temp files..."
remove-item "$($userprofile.fullname)\local settings\temp\*" -force -recurse
remove-item "$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*" -force -recurse
}
}
Ideally I'd like to instantiate IE on the remote box and delete the temp files the right way, but I don't know if that's possible in PS. Any better ideas?
Answers
- ok, here is another way to avoid this limit - use FileSystemObject COM object:
$fso = New-Object -ComObject Scripting.FileSystemObject $fso.DeleteFolder("path to IE cache folder", $true)
http://www.sysadmins.lv- Marked As Answer byKeith W. Thursday, October 29, 2009 2:27 PM
$fso|get-member
And then you will see a method called deletefile.
Does it work?- Marked As Answer byKeith W. Thursday, October 29, 2009 2:28 PM
All Replies
- Well, IE has a COM automation interface:
PS>$ie=new-object -com internetexplorer.application
I don't see a way to automate clearing the temp files though from that object though.
Invoking this remotely could also be an issue, depending on the approach you're considering if you want to try using this COM approach.
So, in the end, is the only problem the fact that some longer file names are spitting out errors? That's the only reason you won't try to delete the files directly? - I am deleting the files directly and it is working aside from the long file names, but when I show my manager my spiffy new Powershell script and he runs it, he's going to get a page full of error messages. And all the files will not be deleted which makes me think I'm just delaying the problem until all those overly long file names fill up the disk.
It's just a can of worms I'd rather not open - I'd like it nice and clean. - ok, here is another way to avoid this limit - use FileSystemObject COM object:
$fso = New-Object -ComObject Scripting.FileSystemObject $fso.DeleteFolder("path to IE cache folder", $true)
http://www.sysadmins.lv- Marked As Answer byKeith W. Thursday, October 29, 2009 2:27 PM
- Thanks Vadims. Is there a switch or something to delete the contents, not the folder?
I tried using "delete" with the [path]\* and it kicks back an error: Com object {hex value} doesn't contain a method named 'Delete'. $fso|get-member
And then you will see a method called deletefile.
Does it work?- Marked As Answer byKeith W. Thursday, October 29, 2009 2:28 PM
- Ah I didn't think about doing the get-member on the variable. This seems to work (although I still get access denied for the logged on user and the network service account). guess that's the best I can hope for. :)
Thanks guys!
# TermScrubber.ps1
# ===================
#
# Connects to each terminal server and cleans off the
# temporary internet files.
#
#
# Declare the servers
# ===================
# $tservers = [real machines]
# @ TESTING BLOCK
# @
$tservers = [test machines]
# @
# @
# Instantiate a file system object
# (this gets around a problem remove-item has with
# very long file names like we find in the temp folders)
# ======================================================
$fso = New-Object -ComObject Scripting.FileSystemObject
# Display a little info on how many profiles you find
# ==================================================
foreach ($server in $tservers) {
write ''
write "Server: $Server"
$profiles = get-childitem "\\$server\C$\Documents and Settings" -force
write "Found $($profiles.count) profiles on $server."
# Delete the temporary internet files for each
# ============================================
foreach ($userprofile in $profiles) {
write "Deleting $server\$userprofile temp files..."
# remove-item "$($userprofile.fullname)\local settings\temp\*" -force -recurse
# remove-item "$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*" -force -recurse
$fso.deletefile("$($userprofile.fullname)\local settings\temp\*", $true)
$fso.deletefile("$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*", $true)
}
}
write ""
write "Work complete!" - Ok I'm getting a strange problem now. I added in a little error checking and now the fso.deletefile stopped working. I see it cycle through every profile but it's not showing an error or actually deleting anything. When I got back to remove-item, it does delete the files (but i get the file length errors again)...
Gah!
# TermScrubber.ps1
# ===================
<#
.SYNOPSIS
Connects to each terminal server and cleans off the
temporary internet files.
.DESCRIPTION
This script connects to a series of servers ($tservers) and locates
each profile under C:\Documents and Settings. Then deletes the content
of the following folders from each profile:
[profile]\local settings\temp
[profile]\local settings\temporary internet files\content.ie5
It does this using a COM object (FileSystemObject) instead of the
standard "remove-item" commandlet to avoid the path length limitations.
.NOTES
File name: TermScrubber.ps1
.KNOWN ISSUES
This script will not delete the contents of temp files for profiles
that are currently in use. Not really an issue but it does result
in error messages.
#>
# Declare the servers
# ===================
$tservers = [testbox]
# Instantiate a file system object
# (this gets around a problem remove-item has with
# very long file names like we find in the temp folders)
# ======================================================
$fso = New-Object -ComObject Scripting.FileSystemObject
# Display a little info on how many profiles you find
# ==================================================
foreach ($server in $tservers) {
write ''
write "Server: $Server"
$profiles = get-childitem "\\$server\C$\Documents and Settings" -force
write "Found $($profiles.count) profiles on $server."
# Delete the temporary internet files for each
# ============================================
foreach ($userprofile in $profiles) {
write "Deleting $server\$userprofile temp files..."
if (test-path "$($userprofile.fullname)\local settings\temp")
{$fso.deletefile("$($userprofile.fullname)\local settings\temp\*", $true)}
Else {write "ERROR: $($userprofile.fullname)\local settings\temp does not exist!" -foregroundcolor red}
if (test-path "$($userprofile.fullname)\local settings\temporary internet files\content.ie5")
{$fso.deletefile("$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*", $true)}
Else {write "ERROR: $($userprofile.fullname)\local settings\temporary internet files\content.ie5 does not exist!" -foregroundcolor red}
#if (test-path "$($userprofile.fullname)\local settings\temp")
# {remove-item "$($userprofile.fullname)\local settings\temp\*" -force -recurse}
# Else {write "ERROR: $($userprofile.fullname)\local settings\temp does not exist!"}
#if (test-path "$($userprofile.fullname)\local settings\temporary internet files\content.ie5")
# {remove-item "$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*" -force -recurse}
# Else {write "ERROR: $($userprofile.fullname)\local settings\temporary internet files\content.ie5 does not exist!"}
}
}
write ""
write "Work complete!"

