How do I delete a folders contents using PDQ Deploy?

Comments

9 comments

  • Brigg Angus

    This is due to the design of Deploy. When you run a command from Deploy, it uses a particular user (unless defined elsewhere, the user is in Preferences -> Credentials).

    So, when you run the above command, it deletes c:\Users\<PDQuser>\AppData\Local\SARSAnywhere\*.* rather than the local logged on user.

    You can test this by creating c:\Users\<PDQuser>\AppData\Local\SARSAnywhere\test.txt on a target machine then running the script as written in Deploy and you should get a success code.

    In order to correct this, change the "Run as" field in the deployment to "Logged on User". This should run the deployment as the user currently logged into the machine and thus remove the contents of that directory.

    If you want to delete those files for all user profiles on the machine, I was able to find this, which isn’t tested or supported, but should provide some guidance on how to do what you want: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/f18bfcfe-79c1-42f8-ad76-888b6ecc83a4/delete-contents-of-a-folder-across-all-users?forum=winserverpowershell

    0
    Comment actions Permalink
  • Scott Mikules

    Using @Brigg Angus's suggestion, I'm trying to remove the Skype folder from %AppData% from the users folders after I have PDQ removed Skype from their amchines using teh following:

     

    PDQ Step 2 process after removing Skype from a machine:

    -----------------------cut-----------------------

    $users = Get-ChildItem C:\Users
    foreach ($user in $users){
    $folder = "$($user.fullname)\AppData\Roaming\Skype"
    If (Test-Path $folder) {
    Remove-Item $folder -Recurse -Force -ErrorAction silentlycontinue
    }
    }

    -----------------------cut-----------------------

    Sadly it's not removing the folder. Not coming back with an error, either. Also: I would like to have this remove ALL Skype folders on all profiles on the C: drive. 

    What am I doing wrong?

    0
    Comment actions Permalink
  • Stephen Valdinger

    Hmmm. Interesting. I just tested your code. Works on my system in a test Scenario.

     

    I generated a test folder structure under c:\temp that mirrors the C:\users\*user*\appdata....etc etc etc

    In one user's folder I created a Skype folder and dumped some junk txt files in it. 

     

    I ran your code, changing Get-ChildItem C;\users to my path instead, and it went through and removed the Skype folder as it should.

     

    Do you use Skype on your systems? I can see if it is actively running on a user's system, that some files will be locked and not able to be deleted. By saying -ErrorAction SilentlyContinue, you won't be able to see that. 

     

    I would suggest you use a Try/Catch block on a test system and capture the output.

     

    Try { 

       Remove-Item $folder -Recurse -Force

    }



    Catch {

       $_.Exception.Message

    }
    0
    Comment actions Permalink
  • Stephen Valdinger

    If the errors mention anything about not able to delete files because they are in use, you'll want to put some logic at the top of the script to stop any Skype Processes

    *note* I'm not 100% what the process names for skype are, as I don't use it.also, I don't recall if piping to stop-process works when there are multiple entries returned, so you may need to pipe to Foreach-Object and throw Stop-Process in the loop.

    Get-Process | Where { $_.Name -match "Skype" } | Stop-Process

    #If Loop is needed:
    Get-Process | Where {$_.Name -match "Skype" } | % {Stop-Process $_}
    0
    Comment actions Permalink
  • Scott Mikules

     

    After digging around and placing a "\" after "Skype" in the folder name, it worked:

     

    $folder = "$($user.fullname)\AppData\Roaming\Skype\"

    Appears without closing the folder it wouldn't' remove it. wonder why you'res worked without it ?

    0
    Comment actions Permalink
  • Stephen Valdinger

    Actually, I think I put it in there naturally come to think of it. That would explain it!

    0
    Comment actions Permalink
  • Jon

    This is very helpful to me as I've been looking for a way to delete folders from each user's folder.  I modified the path and works perfectly, so thank you!

    Would there be a way to get it to bypass the logged on user?

    In my particular case if I delete the folders in question while they are logged in, their application will have errors.  Therefore, I'd like to skip the user's profile who is currently logged in.  The PC's I need this to run on are used 24/7/365 and are shared by a pool of users.  Any of the PC's can be used and logged onto at any point during the day.

    Would it be better to just run this at boot/shutdown/logout as a scheduled task on the individual PC's instead of using PDQ Deploy? 

    0
    Comment actions Permalink
  • Scott Mikules

    @Jon

    If the application you're talking about can be uninstalled while the user is logged in I would do that first (kill the software process, then uninstall it) you can then remove the folder.

     

    that's assuming the application can be removed from the machine, i.e. not needed

    0
    Comment actions Permalink
  • Jon

    Hey Scott...in my case the application is still needed and used almost constantly.

    The problem with it is that it leaves files on the hard drive after exiting and doesn't clean them up.  I asked the developer to look into this and they've pretty much ignored me, so I'm trying to come up with my own work around.  

    Basically, I'm trying to clean up after someone else's mess.  :-)

     

    0
    Comment actions Permalink

Please sign in to leave a comment.