This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Path Folder count monitoring

Hi All,

I am looking for a PowerShell script that can check path and count how many folder available in that path

example i have a path : \\XXXXSSQLP01\Team\work 
if we have 2 or more folders available in that path alert will triggred

can any one help in this


Sumit Goel

  • This is more than an alert.  You need to first build a monitor in SAM to check for the folder count.  There's an out of the box one that does it using PowerShell, but this is basically what it does.

    $Folders = Get-ChildItem -path \\ServerName\SharedFolder\ChildPath -Recurse -Folder
    if ( $Folders.Count -ge 2 ) {
        Write-Host "Message: We found $( $Folders.Count ) Folders - that's bad"
        Write-Host "Statistic: $( $Folders.Count )"
    else {
        Write-Host "Message: We found $( $Folders.Count ) Folders - that's good"
        Write-Host "Statistic: $( $Folders.Count )"

    Then you just need to alter the definition of the component so that if the statistic is 2 or more, it returns a critical status.

    Then the alert is easy: Alert when a component name is "Whatever you called this component" and the status is "Critical" fire an alert.

  • sorry but i am looking for file count monitoring for latest date

    i have prepared some thing but not able to print latest count with date

    get-childitem '\\NJIOILMSSQLU01\ilmsdata\VendorExport\NachaExport' |
    select @{n='Date';e={$_.LastWriteTime.ToShortDateString()}} |
    Group-Object Date |
    Format-Table Name, Count

    IF ($Count -gt 2)
    $Message = 'there are wrong/duplicate files generated'
    Write-Host 'Statistic.Status: '2
    Write-Host 'Message.Status: '$Message
    $Message = 'there are no wrong/duplicate files generated'
    Write-Host 'Statistic.Status: '1
    Write-Host 'Message.Status: '$Message

    ######## End Script #########

  • Are you only looking for files after "today" or in the last 24 hours or something else?  Please be as specific as possible so we can help craft this.

  • for current date file count
    Name Count
    5/31/2022 2

Reply Children
  • Here is a way to show both ways (since midnight today and within the last 24 hours)

    $Path = "\\Path\To\Check"
    # Things since midnight today
    $Today = Get-Date | Select-Object -Property Date | Get-Date
    $FilesToday = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Today }
    # Things since this time yesterday
    $Last24Hours = ( Get-Date ).AddDays(-1)
    $FilesLast24Hours = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Last24Hours }
    # Results for 'today'
    Write-Host "Message.Today: Found $( $FilesToday.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"
    Write-Host "Statistic.Today: $( $FilesToday.Count )"
    # Results for 'Last 24 hours'
    Write-Host "Message.Last24Hours: Found $( $FilesLast24Hours.Count ) files in $( $Path ) created on or after $( $Last24Hours.ToShortDateString() ) $( $Last24Hours.ToShortTimeString() )"
    Write-Host "Statistic.Last24Hours: $( $FilesLast24Hours.Count )"