PowerShell to delete all files but 1 "which looks like a folder" in a dir? Actual Written Script please. RRS feed

  • Question

  • https://social.technet.microsoft.com/Forums/ie/en-US/06d78ef1-4e68-4b0d-9447-bb29f57c46b6/powershell-to-delete-all-files-but-1-in-a-dir?forum=winserverpowershell 

    for :

    $folders = Get-ChildItem c:\temp\test -Recurse -Directory
    foreach($folder in $folders.fullname){
        Write-Host "Clearing in $folder"
        Get-ChildItem -Path $folder -Filter '*.txt' | Select-Object -Skip 1 | Remove-Item

    how is this written for folder C:\Users\jdoe\appdata     and "file" (actually a folder) named


    Yes, it have 3 files in that folder 

     I am not a PS user, but I am trying to learn. Thank You

    Friday, December 27, 2019 12:32 AM

All replies

  • No idea what you are trying to ask. Read the following and try to make the question clear.

    If you are having trouble with English try to find someone to help you.  Technical translations for many languages to English can be a problem as the English will not be clear.  If you language is English then please use full correctly form sentences so we don't have to guess.  Remember that we cannot see your computer or read your mind.


    Friday, December 27, 2019 12:45 AM