# Temp File Deleter # Deletes temp files on Remote PC's # Benjamin Crew 27/06/2017 cd $HOME Invoke-Command -ComputerName "xxxxxxx" -ScriptBlock { Set-Location "C:\Windows\Temp" Remove-Item "*" -Force -Recurse -ErrorAction SilentlyContinue Set-Location "C:\Users" Remove-Item ".\*\AppData\Local\Temp" -Force -Recurse -ErrorAction SilentlyContinue } Write-Host "Script Complete." -ForegroundColor Cyan
Hi all,
I’m brand new to Powershell. I’m on chapter 3 of ‘Powershell in a month of lunches’, and this is the first script I’ve created.
It’s used to make my life on the Service Desk (albeit i’ll only be here a few more weeks before starting a new role) a little easier.
I know it’s very simple, but if anybody has any questions about it, then please let me know.
My question is as follows:
We have a specific format for usernames on our domain. “123456” is an example. 6 characters, all numeric.
My script runs on all user profiles in C:\Users, and I’m trying to find a way to exclude certain folders from the script. For instance, in C:\Users, we have folders named ‘Administrator’, ‘Default’ and ‘Public’.
My first thought was to get rid of the * in “.*\AppData\Local\Temp”, and replace it with ‘$$$$$$’, which would work I guess, but public falls into that category.
I’m sure there is a better way for me to exclude specific folders. Can anybody assist?
P.S. I’m fairly certain that deleting Temp files from these ‘special’ profiles isn’t going to be an issue, but I want to limit the chance of any error messages appearing. I’ve managed to minimise the errors I get from files being locked etc, by adding -SilentlyContinue to the end of rows 10 and 13, but an error that has appeared fairly regularly is ‘An object at the specified path C:\Users\Default User\AppData\Local\Temp does not exist.’ If my script was set to exclude this folder, then I wouldn’t get this error, and I’d be able to share it with the rest of the Service Desk and not cause anybody to panic.
Any help would be massively appreciated.