Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

How do I delete a folders contents using PDQ Deploy?

I would like to delete the contents of the c:\Users\%username%\AppData\Local\SARSAnywhere\ directory so that SARS can update itself when the user launches the program the next time.  The command line I am using is 

del /f /s /q "c:\Users\%username%\AppData\Local\SARSAnywhere\*.*"

It comes back with an error 1 in PDQ and the files are not deleted but if I copy and paste that into a Command Prompt on my target machine, it works fine.  I have even tried a .bat file in PDQ and on the target desktop.  It works on the target desktop but not through PDQ.  Any suggestions would be most helpful.

0

Comments

6 comments
Date Votes
  • 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
  • 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
  • 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
  • Actually, I think I put it in there naturally come to think of it. That would explain it!

    0
  • 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
  • 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