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.
8
  • 1
    How long are you waiting? A command like that can take a while. Commented Dec 9, 2015 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. Commented Dec 9, 2015 at 19:31
  • use the debugger to step thru the script and see what line hangs Commented Dec 9, 2015 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 :) Commented Dec 10, 2015 at 11:34
  • 1
    @JohnLBevan Your fix worked, please submit it as an answer and I will accept it as a solution accordingly. Thanks Commented Jan 4, 2016 at 19:27

1 Answer 1

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 $_ 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.