- # Get-FileCount.ps1
- # Uses shell.application to get a count of files in a folder
- # Thomas Lee - tfl@psp.co.uk
- # First get shell object
- $Shell = new-object -com Shell.Application
- # Now get folder details and item counts for a folder
- $foldername = "D:\Foo"
- $folder = $shell.namespace($foldername)
- if (!$folder) {
- "Folder: {0} does not exist" -f $foldername
- return
- }
- # Now output the count of folders
- if ($folder) {
- $folderitems = $folder.items()
- $count = $folderitems.count
- $folderps1items = $folderitems | where {$_.type -eq "PS1 File"}
- $countps1items=$folderps1items.count
- "Folder `"{0}`" contains {1} files" -f $foldername, $count
- "Folder `"{0}`" contains {1} PS1 files" -f $foldername, $countps1items
- }
This script produces the following output:
PSH [D:\foo]: .\shell.application\Get-FileCount.PS1'
Folder "D:\Foo" contains 38 files
Folder "D:\Foo" contains 22 PS1 files