# Script does not exit but loops

**URL:** <https://forums.powershell.org/t/script-does-not-exit-but-loops/26261>\
**Category:** PowerShell Help\
**Created:** [December 10, 2025, 8:07pm UTC](https://forums.powershell.org/t/script-does-not-exit-but-loops/26261 "2025-12-10T20:07:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 10, 2025, 8:52pm UTC](https://forums.powershell.org/t/script-does-not-exit-but-loops/26261/2 "2025-12-10T20:52:43Z")

</div>

Eric,  
Welcome to the forum. 😉

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[Guide to Posting Code - Redux](https://forums.powershell.org/t/guide-to-posting-code-redux/15797) \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

I’d _streamline_ your PowerShell code a little to avoid reading the directory twice but there’s actually nothing in your PowerShell code indicating that it should loop for some reason.

```auto
$CompleteFileList = 
    Get-ChildItem . -File 
$oldestDate =
    ($CompleteFileList |
        Sort-Object -Property CreationTime |
            Select-Object -First 1).CreationTime.Date
$CompleteFileList |
    Where-Object {
            $_.CreationTime.Date -eq $oldestDate
        } | 
            Remove-Item -WhatIf              

```

It might help when you elaborate a little more how you run your code and especially why you’re running the PowerShell code from inside a batch file. That’s in the vast majority of the cases I know of unnessecary and error prone.

---

_[View the full topic](https://forums.powershell.org/t/script-does-not-exit-but-loops/26261)._
