Get-ChildItem childish behavior or is it just me?


  • Hi all, newbie @ PS so I'm calling in for some help :-)
    (I posted this at r/powershell but didn't get a good reso

    So I've created this script to delete files older then x and remove empty directories.

    It seems to work fine if the Directory in $Cleanup_Path actually exists.


    # Sets the root folder path to perform the cleanup.
    $Cleanup_Path = "C:\Daily Reports\"
    # Sets minimum age of files.  You must use a negative (-) number.
    $Maximum_Days = "-60"
    # Get the current date and time.
    $Current_Date = Get-Date
    # Calculates the date to purge files from for any file less than the time calculated.
    $Delete_Date = $Current_Date.AddDays($Maximum_Days)
    #Displays the calculated Delete_date.
    Write-Host `n`nDeleting files prior to: `n$Delete_Date`n`n
    #sleep 5
    # Deletes files only (not folders) older then the calculated date.  This includes hidden and system files (-Force switch).
    Get-ChildItem -Path "$Cleanup_Path" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $Delete_Date } | Remove-Item -Force -whatif
    # Deletes any empty directories left behind after removing the old files.
    Get-ChildItem -Path $Cleanup_Path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse -whatif


    ...But my problem comes into play when the folder doesn't exists. For some reason it starts processing against all folders in the root of the drive (c:\ in this case).



    Get-ChildItem : Access to the path 'C:\$Recycle.Bin\S-1-5-21-3079630031-1785565312-3449296362-500' is denied. At line:18 char:1 + Get-ChildItem -Path "$CleanupPath" -Recurse -Force | Where-Object { !$.PSIsCon ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\$Recycle.Bin...-3449296362-500:Strin g) [Get-ChildItem], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.G etChildItemCommand


    Get-ChildItem : Access to the path 'C:\Documents and Settings' is denied. At line:18 char:1 + Get-ChildItem -Path "$CleanupPath" -Recurse -Force | Where-Object { !$.PSIsCon ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\Documents and Settings:String) [Get- ChildItem], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.G etChildItemCommand


    If I add a backslash to the end of the path

    $Cleanup_Path = "C:\Daily Reports\"


    ...I get this:



    Get-ChildItem : Cannot find path 'C:\D' because it does not exist. At line:18 char:1 + Get-ChildItem -Path "$CleanupPath" -Recurse -Force | Where-Object { !$.PSIsCon ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\D:String) [Get-ChildItem], ItemNotFoun dException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCom mand


    So as you can see, it gives me an error that it can't find 'C:\D'. So that's fine because now it isn't processing all folders in the root when "Daily Reports" doesn't exist but with my OCD in full tilt, what if I actually have a "C:\D" drive??? Well it will actually process against that!!!


    So what the heck???

    Thursday, April 20, 2017 12:08 AM

All replies

  • Please do not post images of code.  Post code using the code posting tool.

    Read the following for help:

    help Test-Path -full


    Thursday, April 20, 2017 12:17 AM
  • Thursday, April 20, 2017 9:29 AM
  • It's not an image.  It is text formatted differently.  Regardless, I'll use the built in code block feature.

    Thanks for your suggestion with test-path but I already use this to validate paths in other scripts but my question was more of a curiosity of why it behaves this way.  Seems odd or inconsistent to me.


    Thursday, April 20, 2017 5:19 PM
  • Pawel,

    Thank you for the reference to the other post.  I think that answers the first part of my observation in terms of it searching through directories.  But I'm not sure it explains the second part where...

    If I add a backslash to the end of the path:

    $Cleanup_Path = "C:\Daily Reports\"

    ...I get this:

    Get-ChildItem : Cannot find path 'C:\D' because it does not exist...blah blah blah

    Notice how it just has the first character "D" of the directory name "Daily Reports".  And if "c:\D\" actually exists, it will perform the command on that.

    Any thoughts on that or does the other forum post answer that (maybe it just wasn't clear to me)?

    Thank you!

    Thursday, April 20, 2017 5:26 PM
  • I assume that what's happening is PowerShell is splitting the path and treating the last token as a name to search for in all the subdirectories of the parent path.  You can get around this behavior by using a path of "c:\temp\*" instead.  Both of the following commands return the same error, if Temp doesn't exist.

    Friday, April 21, 2017 8:53 AM