Search code examples
powershellskipget-childitem

PowerShell Get-ChildItem and Skip


I'm writing a PowerShell script that deletes all but the X most recent folders, excluding a folder named Data. My statement to gather the folders to delete looks like this:

$folders1 = Get-ChildItem $parentFolderName | 
     ? { $_.PSIsContainer -and $_.Name -ne "Data" } | 
     sort CreationTime -desc | 
     select -Skip $numberOfFoldersToKeep

foreach ($objItem in $folders1) {
    Write-Host $webServerLocation\$objItem
    Remove-Item -Recurse -Force $parentFolderName\$objItem -WhatIf
}

This works great when I pass a $numberOfFoldersToKeep that is fewer than the number of folders in the starting directory $parentFolderName. For example, with 5 subdirectories in my target folder, this works as expected:

myScript.ps1 C:\StartingFolder 3

But if I were to pass a high number of folders to skip, my statement seems to return the value of $parentFolderName itself! So this won't work:

myScript.ps1 C:\StartingFolder 15

Because the skip variable exceeds the number of items in the Get-ChildItem collection, the script tries to delete C:\StartingFolder\ which was not what I expected at all.

What am I doing wrong?


Solution

  • try this:

    $folders1 = Get-ChildItem $parentFolderName | 
         ? { $_.PSIsContainer -and $_.Name -ne "Data" } | 
         sort CreationTime -desc | 
         select -Skip $numberOfFoldersToKeep
        if ($folder1 -neq $null)
    
    {
    
    foreach ($objItem in $folders1) {
    
    
            Write-Host $($objItem.fullname)
    
    
    
    
     Remove-Item -Recurse -Force $objItem.fullname -WhatIf
        }
        }