Vibhav MS
Vibhav MS

Reputation: 163

Search multiple folder to check if they are empty using powershell

I have a bunch of directories

C:\RI1

C:\RI2

C:\RI3

... C:\RI21

How can I check if they are all empty? I want to go further into the script only if one or more of them have files. If not, I want to exit. I tried this but it is searching for folder names and is giving me 21 as the answer

$directoryInfo = Get-ChildItem C:\RI* | Measure-Object
$directoryInfo.count

if ($directoryInfo.count -eq 0)
{

    Write-host "Empty"
}

else
{
    Write-host "Not Empty"
}

Upvotes: 0

Views: 646

Answers (1)

David
David

Reputation: 621

When you run Get-ChildItem C:\RI* you get all the child items in C:\ and filter the results with items which begin with "RI". You get the answer 21 since there are 21 folders in C:\ that starts with "RI".

I suggest that you run through all the folders using a foreach loop.

$folders = @("RI1", "RI2", "RI3")

foreach ($folder in $folders)
{
    $path = "C:\$folder"
    $directoryInfo = Get-ChildItem $path
    if ($directoryInfo.count -eq 0)
    {
        Write-Host "Empty"
    }
    else
    {
        Write-Host "Not Empty"
    }
}

Upvotes: 1

Related Questions