PS script error

Hram Admin 290 Reputation points
2025-04-15T09:48:39.45+00:00

Hello!

I have a few scripts that delete old backups from some folders. There's actually one string that deletes the oldest folder:

Remove-Item -Path [Folder Name] -Recurse -Force

...and it works perfect for all folders except the one: while deleting this folder the script terminates with the error 0xC00000FC or - if run in ISE - the ISE itself exits and gets restarted.

This folder is different from any other only in containing user profiles, but the script is run with the #Requiers Administrator option so it must have the right to delete them.

Either script or ISE doesn't even delete anything - the folder size never changes, it seems the script just hangs for ~5-7 minutes and then crashes with 0xC00000FC.

What can be the cause of this?

Regards,
Michael

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Hram Admin 290 Reputation points
    2025-04-24T11:01:27.75+00:00

    Yes, Remove-Item can also throw PathTooLongException - this is the cause of the problem, but in most cases PS just hanges or restarts :(

    Thank you all for your help!!!

    0 comments No comments

  2. MotoX80 37,156 Reputation points
    2025-04-25T19:21:35.53+00:00

    Ah yes, the long file name "feature". Windows supports long file names but since most apps use old API's they still can't reference long file names.

    Powershell does not have that problem. I believe that robocopy can handle long file names too.

    In any event, if you have LongPathsEnabled set to 1, then you should be able to use long file names. If you don't have it enabled, my testing has found that you can still access long file names but you have to preface the name with "\\?\".

    You can use this script to test your pc's. Note that Remove-Item successfully deletes the "aaaa" folder structure.

    #----------------------------------------------
    # Script: LongFileTest.ps1
    # Author: Motox80 on Microsoft Technet Forums
    #----------------------------------------------
    #
    # Does Windows support long file names? 
    #
    cls
    $a = ('a' * 100) 
    $b = ('b' * 100) 
    $c = ('c' * 100)
    $d = ('d' * 100)
    function Testit {
    	""
    	"Testing base folder name: {0}" -f $base
    	""
    	try {
    		Set-Location $base 
    		New-Item  $a -ItemType Directory -ErrorAction Stop
    		Set-Location $a
    		"Test data" | out-file TestFile.txt
    		get-childitem
    		New-Item  $b -ItemType Directory -ErrorAction Stop
    		Set-Location $b
    		"Test data" | out-file TestFile.txt
    		get-childitem
    		New-Item  $c -ItemType Directory -ErrorAction Stop
    		Set-Location $c
    		"Test data" | out-file TestFile.txt
    		get-childitem
    		New-Item  $d -ItemType Directory -ErrorAction Stop
    		Set-Location $d
    		"Test data" | out-file TestFile.txt
    		get-childitem
            ""
    		"File name length = {0}" -f ((get-item TestFile.txt).fullname.length) 
    	} catch  {
        		write-host -ForegroundColor red  "New-item failed!" 
        		$_.exception 
    	} 
    	Set-Location $base
        Remove-Item $a -recurse -force 
    	"Test folder was deleted."
    }
    "Testing HKey_Local_Machine\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled"
    $x = get-itemproperty HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem  -name LongPathsEnabled
    if ($x.LongPathsEnabled -eq 1) {
        "Long file names are enabled in the registry."	
    } else { 
        write-host -ForegroundColor red "Long file names are NOT enabled in the registry." 
        write-host -ForegroundColor red "This may not work." 
    }
    if ((Get-Process -id $pid).ProcessName -eq "powershell_ise") {
        ""
        write-host -ForegroundColor red -BackgroundColor Yellow "---------------------------------------------"
        write-host -ForegroundColor red -BackgroundColor Yellow  "This will not work when using PowerShell_ISE."
        write-host -ForegroundColor red -BackgroundColor Yellow "---------------------------------------------"
        return
    }
    $base = "\\?\" + $env:TEMP    # tells the Windows APIs to disable all string parsing 
    Testit
    $base = $env:TEMP 
    Testit
    "Test complete "
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.