0

I wrote the following script to delete all temp files and cached data from users profiles located on network share.

It works fine when I run it locally on my machine but when I attempt to run it on a network share it always fails.

This is how my script works,

  1. specify a list of folder that needs cleanup names.text
  2. specify the path for the directory
  3. use remove-item command to delete the required files

     $userlist = Get-Content -Path "C:\Profiles\names.txt"
     $home_folders = "C:\Profiles\"
    
    
     $UserList | ForEach-Object {    
     $User_Home = $Home_Folders + "\" + $_
    
     Remove-Item "$User_Home\UPM_Profile\AppData\Local\Temp\*" -Force -Recurse 
    }   
    

What I have done so far?

  • I tried placing the path as \UNC path but that does nothing.
  • I tried to remove -erroraction silentlycontinue to see what kind of errors shows up but it does nothing. like I execute the script but it just hang.
  • I also tried to execute the script from the same network share but without luck.
user2398069
  • 103
  • 1
  • 3
  • 1
    How long are you waiting? A command like that can take a while. – Colyn1337 Dec 09 '15 at 19:01
  • 1
    So the item you are removing will be `C:\Profiles\\someName\UPM_Profile\AppData\Local\Temp\*` ? Even better, instead of guessing, create a variable $itemToRemove, and use Write-Host to display each item that is removed. Writing and debugging scripts is difficult with no logging/information. – Greg Askew Dec 09 '15 at 19:31
  • use the debugger to step thru the script and see what line hangs – Jim B Dec 09 '15 at 20:17
  • `Appdata\Local` is specifically designed for data that does not need to persist between sessions. Why not exclude `AppData\Local` and `AppData\LocalLow` in the Citrix Profile Management policy? Then you don't have to perform the cleanup in the future :) – abstrask Dec 10 '15 at 11:34
  • Not a solution to your issue; but you may want to replace `$User_Home = $Home_Folders + "\" + $_` with ` $User_Home = Join-Path $Home_Folders $_`; that'll avoid any issues with additional slashes. – JohnLBevan Dec 12 '15 at 04:24
  • As previously mentioned, you have a double slash in your $User_Home variable as the script stands. I removed the added slash on the variable creation line and ran this script just fine referencing both a local drive and a UNC path. Can you be more specific about how it "fails"? Do you get errors? If so, what are they? Any information would be helpful in troubleshooting this issue. – McKenning Dec 21 '15 at 23:23
  • 1
    @JohnLBevan Your fix worked, please submit it as an answer and I will accept it as a solution accordingly. Thanks – user2398069 Jan 04 '16 at 19:27
  • Great news; glad it worked; hadn't realised that it would actually resolve your issue, but glad that it did. :) – JohnLBevan Jan 04 '16 at 20:25

1 Answers1

0

You're currently appending two backslashes to your directory:

$home_folders = "C:\Profiles\"
#...
$User_Home = $Home_Folders + "\" + $_

You could get around this by removing the slash from home_folders ($home_folders = "C:\Profiles) or by removing it from your join ($User_Home = $Home_Folders + $_).

However a better way is to use the Join-Path cmdlet, which checks for a slash and appends one as needed:

$User_Home = Join-Path $Home_Folders $_
JohnLBevan
  • 1,134
  • 7
  • 20
  • 44