Trying to Delete C:\Users\appdata\local\Temp\*.*

This topic contains 3 replies, has 3 voices, and was last updated by  Olaf Soyk 3 months, 1 week ago.

  • Author
    Posts
  • #91325

    Monty
    Participant

    I am trying to Delete C:\Users\Username\appdata\local\Temp\*.*.

    The following only works for my account. I am building the script so this is only testing. I get a listing for my profile and a failure for the Public profile because the directory doesn't exist. But I don't get squat for any other profiles. This is on a Windows 10 computer but I would like it to work on Windows 7 also.

    Get-ChildItem "C:\Users" | ForEach-Object {Get-ChildItem (Set-Location "C:\Users\$($_.name)\appdata\local\temp")}
  • #91327

    Justin King
    Participant

    This is a security issue, not a PowerShell issue per say. By default, a users profile only has the following permissions:

    SYSTEM
    (owning user)
    Administrators

    Now the local Administrators group is a well-known protected group, so if you're UAC is enabled, then you will not have that permission and thus access will be denied. Test your script with administrator access (right-click run as administrator) to confirm, or look at the path permissions to validate.

    • #91331

      Olaf Soyk
      Participant

      Justin,
      the folders marked as system or hidden will be listed only with the parameter -Force anyway. So that does not matter in this case. 😉

  • #91328

    Olaf Soyk
    Participant

    Of course it does not exist. The PUBLIC profile does not have an AppData folder. Simply explude the PUBLIC directory like this:

    Get-ChildItem 'C:\Users' -Exclude 'PUBLIC' |
        ForEach-Object {Get-ChildItem (Set-Location "C:\Users\$($_.name)\appdata\local\temp")}

You must be logged in to reply to this topic.