2

I have been writing a script to inventory drives by specific file types. (AVI, MPG, MP3, etc....)

I can get the base script to work using only a set drive and extension, but I would really like to have it read extensions from a file, and drives from a file.

$dir = get-childitem z:\ –recurse
ForEach ($item in $dir)
{
If ($item.extension –eq '.avi')
    {
    $item | select-object length,fullname,LastWriteTime | Export-CSV  C:\temp\z-avi.csv –notypeinformation –append
    }
}

When I search I am only finding server drivespace scripts.

Any guidance would be appreciated.

3
  • 1
    do you want each disk to have a separate csv for each file type? and is it running on multiple servers?
    – user35213
    Sep 23, 2015 at 15:30
  • Two things to help: A System.IO.FileInfo object has an Extension property. You can group objects easily with Group-Object.
    – jscott
    Sep 23, 2015 at 16:20
  • If it could seperate by drive, or at least include the dive letter, I could create a pivot table based upon the drive.
    – Keith
    Sep 23, 2015 at 22:34

2 Answers 2

0

Messy but I went with WMI to get the drives and then looped through based on unique extensions:

$computer = Get-ADcomputer ComputerName
$drives = Get-WmiObject win32_volume -ComputerName $computer.DNSHostName | Where-Object {$_.DriveType -eq 3 -and $_.DriveLetter -ne $null -and $_.Label -ne "System Reserved"}

Foreach ($drive in $drives)
{
    $allfiles = gci $drive.DriveLetter -recurse | Select Name,FullName,Extension,Length,LastWriteTime
    $extensions = $allfiles | Select -Unique Extension
    Foreach ($ext in $extensions)
    {
    $filename = ($drive | Select -ExpandProperty DriveLetter -First 1)[0] +     ($ext | Select -ExpandProperty Extension -First 1)
    $extensionfiles = $allfiles | Where-Object {$_.Extension -eq   $ext.extension}
    #$extensionfiles.count
    $extensionfiles | Export-Csv C:\Temp\$filename.csv -Notypeinformation
    }
}

The WMI call will bring back only local drives.

2

Something like this should do the trick... Please note the -WhatIf on the Export-Csv bit.

All csv-files would be saved under C:\temp in this example.

$drives = Get-Content .\Drives.txt
$extensions = Get-Content .\Extensions.txt

foreach($drive in $drives) 
{
    $files = Get-ChildItem -Path "$drive`:\*" -Recurse -Include $($extensions | % { "*.$_" }) | where { $_.PSIsContainer -eq $false }
    $grouped = $files | Group-Object -Property Extension
    foreach ($group in $grouped)
    {
        $group | select -ExpandProperty Group | select Length, FullName, LastWriteTime | Export-Csv -Path "C:\Temp\$drive-$($group.Name.Replace('.','')).csv" -Append -NoTypeInformation -WhatIf
    }
}

Where Drives.txt has one drive letter per line

C
D
E
[...]

And Extensions.txt has one extension per line.

mp3
mpg
avi
[...]
2
  • I tried the previous code, and it does not output anything for me in the c:\temp\ folder. I have edited the text files exactly like above, but still get the following message on files. Get-ChildItem : Could not find item x:\Patient Education for Open Heart Surgery Patients Pre-op.docx. At C:\Users\e32045\Desktop\files and sizes to csv with Variables.ps1:6 char:14 + $files = Get-ChildItem -Path "$drive`:*" -Recurse -Include $($extensions | ...
    – Keith
    Sep 23, 2015 at 21:06
  • I assume you deleted the -WhatIf bit first. Anyway, it seem to work just fine for me, both with local and network paths. Could you try doing a cd $drive right before the Get-ChildItem line and see if that makes any difference?
    – notjustme
    Sep 24, 2015 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .