Can my script be editted to exclude certain folders?

This topic contains 9 replies, has 3 voices, and was last updated by  Chris 3 months, 3 weeks ago.

  • Author
    Posts
  • #73742

    Benjamin Crew
    Participant
    # 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.

  • #73745

    Chris
    Participant

    Hi Benjamin,

    There is an -exclude switch that you can use E.G :

    Remove-Item -Recurse  -Path "" -Exclude temp,test.html -Verbose –Force 

    Try and incorperate that into your script, if you are struggling, there should be a fair bit on Google about -exclude.

  • #73747

    Benjamin Crew
    Participant
    Remove-Item ".\*\AppData\Local\Temp" -Force -Recurse -Exclude "Admin", "Administrator", "Public", "Default", "Default User" -ErrorAction SilentlyContinue

    Well, assuming I've laid it out correctly, that was quite a lot simpler than I expected!

    Thank you very much Chris.

    • #73751

      Ivan
      Participant

      This might not apply to your environment, but, sometimes you will see other profiles such as:

      DefaulUser0
      DefaultUser.migrated
      ADMIN~1

      Just something to consider.

      Enjoy PowerShell!! and keep learning more.

    • #73757

      Benjamin Crew
      Participant

      You're right. I see these profiles from time to time. I suppose I can keep adding to the script as I learn of the different profiles.

      The best solution would be to do an IF statement that says, if the profile name isn't 6 characters and numeric, exclude.
      Like I mentioned in my OP, I'm not sure if this is possible and definitely don't know how to do it. About to pick my Powershell book up though, so I'll scan through the index and see if I can find anything out!

  • #73814

    Benjamin Crew
    Participant
    #Exclude#
    
        $Exclude = "Admin", "Administrator", "Public", "Default", "Default User", "ADMIN~1"
    
    #Script Start
    
    cd $HOME
    
    Invoke-Command -ComputerName "xxxxxxxx" -ScriptBlock {
    
    Set-Location "C:\Windows\Temp"
    Remove-Item "*" -Force -Recurse -ErrorAction SilentlyContinue
    
    Set-Location "C:\Users"
    Remove-Item ".\*\AppData\Local\Temp" -Force -Recurse -Exclude "$Exclude" -ErrorAction SilentlyContinue
    
    }
    
    Write-Host "Script Complete." -ForegroundColor Cyan

    Just a follow-up...

    I'm not sure this is going to work as intended.
    Will the line of code above run to all user profiles in C:\Users, head towards \AppData\Local\Temp and THEN exclude the files with names "Admin", "Administrator", "Public", "Default", "Default User", "ADMIN~1"?

    I obviously want it to run the Exclude at the asterix in ".\*\AppData\Local\Temp".

    Any advice?
    Thanks in advance.

  • #73822

    Chris
    Participant

    Hi Benjamin,

    If you aren't sure and want to see the output before it happens, a test run if you will, add -WhatIf after the remove-item cmdlet, so it would look like this:

     Set-Location "C:\Users"
    Remove-Item ".\*\AppData\Local\Temp" -Force -Recurse -Exclude "$Exclude" -ErrorAction SilentlyContinue -WhatIf 
    • #73825

      Benjamin Crew
      Participant

      Thank you very much Chris.

      My suspicions have been confirmed. The -Exclude runs from within the Temp folder.

      You've been a great help, thanks again!

  • #73835

    Benjamin Crew
    Participant

    I now have a working version of the script.

    # Temp File Deleter
    # Deletes temp files on Remote PC's
    # Benjamin Crew 27/06/2017
    
    #Script Start
    
    cd $HOME
    
    Invoke-Command -ComputerName "d0005itsd" -ScriptBlock {
    
    Set-Location "C:\Windows\Temp"
    Remove-Item "*" -Force -Recurse -ErrorAction SilentlyContinue
    
    Set-Location "C:\Users"
    Remove-Item ".\*\AppData\Local\Temp" -Force -Recurse -Exclude "Admin", "Administrator", "Public", "Default", "Default User", "ADMIN~1", "UpdatusUser", "etc", "etc" -ErrorAction SilentlyContinue
    
    }
    
    Write-Host "Script Complete." -ForegroundColor Cyan

    Unfortuntely the array thing that I tried to do didn't work. It would have definitely made my script look a little neater, but I shouldn't mess around with stuff I don't quite understand.

    Thanks for everybodies help.

  • #73837

    Chris
    Participant

    Hi Benjamin,

    Great stuff!

You must be logged in to reply to this topic.