I have folders with different count of files. How to check if there are some files and how many? I use this, but when there is only one file, it gives me nothing. Thank you.
$number_of_files = Get-Childitem c:\folder -name -force
$number_of_files.Count
Change it like this:
$number_of_files = @(Get-Childitem c:\folder -name -force)
$number_of_files.Count
This force $number_of_files
to be always an Array and have the count property rigth set
also for value of 1
The answer from @Marek works, but it perhaps not the most efficient. Rather than doing a .GetType() and doing a regex match, why not just filter based on a property in folder and not in file object. IE:
(Get-ChildItem c:\folder -name -force | where {! $_.PSIsContainer} | measure-object).count.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With