Try this. You can add any amount of folders, file types and file counts to the vairable $FoldersToCheck
:
# File to store log
$LogFile = '.\FileCount.log'
$FoldersToCheck = @(
@{
Path = 'C:\path\to\folder'
FileType = '*.cab'
FileCount = 13
},
@{
Path = 'C:\path\to\folder\subfolder'
FileType = '*.txt'
FileCount = 14
},
@{
Path = 'D:\path\to\some\other\folder'
FileType = '*.log'
FileCount = 15
}
# ... etc, add more hashtables for other folders
)
$FoldersToCheck | ForEach-Object {
$FileCount = (Get-ChildItem -LiteralPath $_.Path -Filter $_.FileType | Where-Object {!($_.PSIsContainer)}).Count
if ($FileCount -eq $_.FileCount)
{
$Result = "Success! Expected $($_.FileCount) file(s) of type $($_.FileType) in folder $($_.Path), found $FileCount files"
}
else
{
$Result = "Failure! Expected $($_.FileCount) file(s) of type $($_.FileType) in folder $($_.Path), found $FileCount files"
}
# Output result to file and pipeline
$Result | Tee-Object -LiteralPath $LogFile
}
Sample output:
Success! Expected 13 file(s) of type *.cab in folder C:\path\to\folder, found 13 files
Failure! Expected 14 file(s) of type *.txt in folder C:\path\to\folder\subfolder, found 10 files
Failure! Expected 15 file(s) of type *.log in folder D:\path\to\some\other\folder, found 18 files