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!
# 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)
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.