Powershell Script to get list of files by type

2790 pts.
Tags:
cmdlet
File Types
IT Scripts
Ok, here is my little contribution to the IT Scripts. I hope someone find it helpful. It is a PowerShell script to collect simple but useful data from a specific directory. When you are searching for some file types, it is so difficult to know what you really have in your hard disk. I think it is no exist a view in Windows Explorer to see your files sorted or grouped by type (regardless of the directory structure) helping you to know how many files do you have of same type and the disk size they are actually using. If this view really exists, please tell me ;) The script is very simple, it do the following things: 1. It collects data from a given directory (and subdirectories). 2. It groups the collected data by file type in all the subdirectories and sort it by type. 3. In case you wish to see the list of a particular filetype, the script provides a list of all the files by a given type, it means the file extension. (log files lost in the disk will be an interesting filetype to test the script) 4. And last but not least, the script shows you a total filesize for a given filetype. Here is a list of some command-lets used and a short description of each. Test-Path. It checks that given directory (or file) really exists. Get-ChilItem. Gets the content of the directory. The "-recurse" flag makes the cmdlet looks for the subdirectories. Sort-Object. This cmdlet sort the output. Group-Object. Groups the collected data by the given parameter. In this case by Extension. Where-Object. Applies the filter whitin {} to the collected data. Powershell is powerful. Its major feature I think is the ability to redirect outputs from one command to another. This is a great feature that system administrators can use now when they are not working on unix platforms :). I hope it helps someone!
Here is the code:
# MENU
Write-Host "Simple script to get some file information from a given directory"
Write-Host " "
do{
Write-Host " "
Write-Host "Choose your option."
Write-Host "1. Collect data for specific directory (and subdirectories)."
Write-Host "2. List summary of total files by type."
Write-Host "3. List files by type (entering the extension)."
Write-Host "4. Totalize filesize by type (entering the extension)."
Write-Host "5. Exit "
$a = Read-Host "Select 1-5: "
 
Write-Host " "
 
switch ($a) 
1 {
$dire = Read-Host "Please type full path of the directory to work with: "
if (Test-Path $dire){
$dir = $dire
"** Collecting data, please wait.. **";
$arr = Get-ChildItem -Recurse $dir
$arrS = $arr | Sort-Object -CaseSensitive
"** done! **";
}else{
Write-Warning "Typed directory doesn't exists."
Write-Host "Please type again."
}
break;
2 {
"** Summary by file types **";
if (!$dir) {
Write-Warning "Please collect data from directory first (option 1)."
}else{
$arrS  | Group-Object Extension | Sort-Object Count
}
break;
3 {
"**Listing files by type (extension) **";   
if (!$dir) {
"Please collect data from directory first (option 1)."
}else{
$ext = Read-Host "Please type the desired extension: "
$arrS | Where-Object {$_.Extension -eq "$ext"}
}
break;
4 {
"** Summary of lenght by file type **";
if (!$dir) {
"Please collect data from directory first (option 1)."
}else{
$ext = Read-Host "Please type the desired extension: "
$items = $arrS | Where-Object {$_.Extension -eq "$ext"} |  ForEach-Object { $_.Length  / 1kb}
foreach ($item in $items){
$totsize += [Int] $item
}
"Total : (kb) $totsize " 
}
" "
break;
5 {
"Thank you."
break;
}
default {
"** Invalid option, please type again. **";
break;
}
}
 }
 while($a -ne 5)


Software/Hardware used:
Windows7, PowerShell

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Mariodlg
    [...] Best Commentary: Mariodlg offered feedback on a couple of requests, in addition to submitting one of his own. [...]
    0 pointsBadges:
    report
  • TechTalker
    Thanks for providing the sample Powershell script. I am looking to write a simple Powershell script to loop through a list of files and filter (grep) out certain patterns and this script gave me the idea to try using Powershell scripting.
    2,310 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following