…
You might be thinking, why we need any script for counting files in the directory as it is just an easy job. Yes I do agree, you can manage to count files on one computer but just think how about if you need to find no. of files under directory on several no. of servers. You have no idea how much time you need to spend to get the entire information for those servers. But the good news is that we can make this task much easier with PowerShell. Yes, we can order “Hey PS, I have a bunch of servers where I need to count files for these directories. Can you do it for me and I am sure it will not refuse to do this. So, the question is how? That’s what the post is about.
Catch a folder for counting files. Let’s say I have created a folder name "MyFolder" that has some files. We will be counting no. of files under MyFolder directory using PowerShell script. Later on, We will see how can we modify the script for another folders name too.
Here, We have two method to count no. of files depeding upon the condition.
If you see the above screenshot, you will find that there are three folders and 7 files with extension (.sys) in the MyFolder directory. So here, we can do two things. We can count the total no. of items combining Sub-folders and files under the directory or we can count the no. of files for specific file extension.
To Get the total no. of objects in the directory. we will use
10
OR We can count the files by filtering extension (.sys)
10
Script for counting files & folders in the "Drivers" directory
$ServersList=Get-Content servers.txt
$Collected_Data =$()
$ErrorActionPreference ='silentlycontinue'
foreach($Server in $ServersList) {
$ServerName=$Server.Trim()
#Path for remote server, you can change your own path after "\c$\yourfolderpath"
$Path="\\" + $Server + "\c$\Windows\System32\drivers"
#for local computer"
#$Path= "C:\Windows\System32\drivers"
$Report =@{"Server Name" =$ServerName;}
Write-Host " "
Write-Host "Now Script is Checking Files on " $ServerName
Write-Host " "
if(!(Test-Connection -Cn $ServerName -Count 1 -ea 0 -quiet))
{ Write-Host "-------------------------"
Write-Host "Problem occured while connecting to Server " $ServerName -ForegroundColor yellow
Write-Host "-------------------------"
$Report["Driver"] = "Unreachable"
New-Object -TypeName PSObject -Property $Report -OutVariable WholeStatus
$Collected_Data +=$wholeStatus
}
else {
$Fetched=Get-ChildItem -Path $Path | Measure-Object
<# Remove comment from below codes if you are using filter for .sys extension.
$Fetched=[System.IO.Directory]::GetFiles("$Path", "*.sys").Count
$Report["Driver"] = $Fetched;
#>
#comment below if you are using filter for .sys extension
$Report["Driver"] = $Fetched.Count
New-Object -TypeName PSObject -Property $Report -OutVariable WholeStatus
$Collected_Data +=$wholeStatus
}
}
$Collected_Data | Export-Csv File_info.csv -NoTypeInformation
Now we will modify the script by adding one more folder eg: Windows\Temp
$ServersList=Get-Content servers.txt
#Initializing scalar varialble to keep object
$Collected_Data =$()
$ErrorActionPreference ='silentlycontinue'
foreach($Server in $ServersList) {
$ServerName=$Server.Trim()
#Path for remote server, you can change your own path after "\c$\yourfolderpath"
$DriverPath ="\\" + $Server + "\c$\Windows\System32\drivers"
$TempPath = "\\" + $Server + "\c$\Windows\Temp"
#for local computer"
#$Path= "C:\Windows\System32\drivers"
$Report =@{"Server Name" =$ServerName;}
Write-Host " "
Write-Host "Now Script is Checking Files on " $ServerName
Write-Host " "
if(!(Test-Connection -Cn $ServerName -Count 1 -ea 0 -quiet))
{ Write-Host "-------------------------"
Write-Host "Problem occured while connecting to Server " $ServerName -ForegroundColor yellow
Write-Host "-------------------------"
$Report["Driver"] = "Unreachable"
$Report["Temp"] = "Unreachable"
New-Object -TypeName PSObject -Property $Report -OutVariable WholeStatus
$Collected_Data +=$wholeStatus
}
else {
$DriverFiles = Get-ChildItem -Path $DriverPath | Measure-Object
$TempFiles = Get-ChildItem -Path $TempPath | Measure-Object
<# Remove comment from below codes if you are using filter for .sys extension.
$Fetched=[System.IO.Directory]::GetFiles("$Path", "*.sys").Count
$Report["Driver"] = $Fetched;
#>
#comment below if you are using filter for .sys extension
$Report["Driver"] = $DriverFiles.Count
$Report["Temp"] = $TempFiles.Count
New-Object -TypeName PSObject -Property $Report -OutVariable WholeStatus
$Collected_Data +=$wholeStatus
}
}
$Collected_Data | Export-Csv File_info.csv -NoTypeInformation
Hi There! My name is Rohit and I am working in the one of MNC as Web Apps developer. I have been in this tech industry for last 5.6 years. This blog is just a part of my career journey.
Ready to make new mistakes without repeating the previous ones.
"All life is an experiment. The more experiments you make the better"