locked
Powershell script OK in Windows 8 - fails in Windows 10 RRS feed

  • Question

  • This script worked perfectly in Windows 7 and Windows 8. It fails in Windows 10.

    # Delete all Files in C:\temp older than 2 day(s)
    $Path = "C:\windows\temp"
    $Daysback = "-2"
    $CurrentDate = Get-Date
    $DatetoDelete = $CurrentDate.AddDays($Daysback)
    Get-ChildItem $Path  | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -force -recurse

    NOW, Powershell Vers 5.1.17134 gets this error:

    Remove-Item : Cannot remove item C:\windows\temp\CreativeCloud\ACC\ACC.log: The process cannot access the file 'ACC.log' because it is being used by another process.
    At line:1 char:78
    + ...  { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -force -recurse
    +                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (ACC.log:FileInfo) [Remove-Item], IOException
        + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
    Remove-Item : Cannot remove item C:\windows\temp\CreativeCloud\ACC: The directory is not empty.
    At line:1 char:78
    + ...  { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -force -recurse
    +                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (ACC:DirectoryInfo) [Remove-Item], IOException
        + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
    Remove-Item : Cannot remove item C:\windows\temp\CreativeCloud: The directory is not empty.
    At line:1 char:78
    + ...  { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -force -recurse
    +                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\windows\temp\CreativeCloud:DirectoryInfo) [Remove-Item], IOException
        + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand


    Rather than moving to the other files in the directory, the program ends. Any suggestions?
    Monday, June 11, 2018 9:37 PM

All replies

  • Please read the error message carefully.  You cannot delete a file that is open.

    Numerous files in the "temp" folder are always open from boot.  They cannot be deleted.  This depends on what is installed in the Windows OS.  W10 has more open files in temp anyway.

    There is no need to manually or by script delete an files in TEMP.  The system cleanup utility is scheduled to cleans this folder periodically

    See the following MS support article for instructions: https://support.microsoft.com/en-us/help/253597/automating-disk-cleanup-tool-in-windows

    This tool has been available since Vista.


    \_(ツ)_/

    Monday, June 11, 2018 9:45 PM
  • Good idea... But, the cleanup program does not see the files in question. That's why I wrote the script.

    Any idea how to fix the script?

    Monday, June 11, 2018 10:41 PM
  • That is because the files in question cannot be deleted.  They are in use by the system.

    TO delete them you will have to stop the service or shut down the system. This is just basic Windows and how it works. "File is in use" cannot be circumvented on any version of Windows.


    \_(ツ)_/

    Monday, June 11, 2018 10:44 PM
  • Sorry for my bad explain. 

    The script used to skip the 'can't delete' and go on to the other deleteable files. There are about 10 per day created.

    Now the script breaks after encountering an in use file. So, the idea is to skip the bad file and delete the next file.

    Can't tell you why it won't work in Win 10 - but did in Win 8.

    Monday, June 11, 2018 11:10 PM
  • The script doesn't break.  It just skips the file.

    To see what is happening do this:

    Get-ChildItem $Path  -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Verbose -Ea 0


    \_(ツ)_/

    Monday, June 11, 2018 11:13 PM
  • That resulted in:

    VERBOSE: Performing the operation "Remove File" on target "C:\windows\temp\HOME-DESK-20180610-0655.log".
    VERBOSE: Performing the operation "Remove File" on target "C:\windows\temp\HOME-DESK-20180610-1839.log".
    VERBOSE: Performing the operation "Remove File" on target "C:\windows\temp\HOME-DESK-20180610-1909.log".
    VERBOSE: Performing the operation "Remove File" on target "C:\windows\temp\MpSigStub.log".

    Great! That worked. So, what is -Ea 0 that causes the script to work?

    Monday, June 11, 2018 11:23 PM
  • That is why you need to learn PowerShell before trying to guess how to use it.


    \_(ツ)_/

    Monday, June 11, 2018 11:44 PM
  • Pretty offensive for a moderator.
    Monday, June 11, 2018 11:53 PM
  • Pretty offensive for a moderator.

    You can be offended by good advice. It is a the best excuse for ignoring it.


    \_(ツ)_/

    Tuesday, June 12, 2018 12:26 AM