Solved

powershell report script by file type

Posted on 2010-09-23
8
2,274 Views
Last Modified: 2012-08-13
Hello everyone I am in need of a powershell script that will list files by extension like .doc .xls, etc...etc.., by file size and then list by the total of the file. I am asking for  script that will list in this fashion

Type of file         Total size of file type  Total amount of type of document
Doc                     622MB                             300
PST                      20GB                              10
ETC...ETC..



I have been playing around with this however, I noticed this do not list pst files. I havel tried the below line

gci -recurse | select -expand extension | group | sort count -Descending

I have also tried this mixture but it produces nothing.

Get-childitem \\fileshare\server -R | Where {$_.Extension -EQ "*.doc"}
Get-childitem \\fileshare\server -R | Where {$_.Extension -EQ "*.docx"}
Get-childitem \\fileshare\server -R | Where {$_.Extension -EQ "*.ppt"}
Get-childitem \\fileshare\server -R | Where {$_.Extension -EQ "*.pptx"}
Get-childitem \\fileshare\server -R | Where {$_.Extension -EQ "*.xls"}


0
Comment
Question by:tdodd72
  • 4
  • 3
8 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33749605
check this from http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_25072410.html
$( $Files = Get-ChildItem "C:\temp" -Filter *.txt
$Files | %{ "$($_.FullName) - $('{0:N2}' -f ($_.Length/1Kb))Kb" }
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $('{0:N2}' -f (($Files | Measure-Object -Sum Length).Sum/1Kb))Kb" )

Open in new window

0
 

Author Comment

by:tdodd72
ID: 33750059
Nice idea I guess I would make this as a function for each file extension. However, when running the code below here is what I am getting


 $( $Files = Get-ChildItem "\\megcifs01\music" -Filter *.MP3
$Files | %{ "$($_.FullName) - $('{0:N2}' -f ($_.Length/1Kb))Kb" }
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $('{0:N2}' -f (($Files | Measure-Object -Sum Length).Sum
$(subexpression) starting:
At line:5 char:15
+ "Total Size: $ <<<< ('{0:N2}' -f (($Files | Measure-Object -Sum Length).Sum
is missing the closing ')'.
At line:5 char:70

The string starting:
At line:5 char:1
+  <<<< "Total Size: $('{0:N2}' -f (($Files | Measure-Object -Sum Length).Sum
is missing the terminator: ".
At line:5 char:70

Missing closing ')' in subexpression.
At line:5 char:70
0
 

Author Comment

by:tdodd72
ID: 33750453
I tried putting ) and } at the end but for some reason still cannot get this to run. I am running powershell 2.0
0
 

Author Comment

by:tdodd72
ID: 33750531
Ok I think I got it. I took some of the code from that site and modified it a little bit.

$( $Files = Get-ChildItem "\\fileshare\sharename" -Recurse -Filter *.mp3
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> OUTPUT.txt
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" )

I did not need the output to display the directories so I directed this to a file. While the results is what I wanted and got this below, there is one other thing I need. So my last and final question is this. I got this to run but now I am having to go back and find every file extension that exists on the server. Is there an automated way to do this because now I am going to be setting up about 20 functions representing each file extension.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:Learnctx
ID: 33751245
You could use an arraylist or hash table to get a list of extensions. Hash table example is below. You might want to play with it and refine it a bit more because you're going to be recursing through the directory structure twice. Once to get the extensions and then once again for the rest of the script. If you spend a but of time on it you can incorporate it all in 1 loop through.

$hashExt = @{}
Get-ChildItem \\fileshare\sharename -Recurse | ? {$_.attributes -notmatch "Directory" -and $_.Extension} | % {
	if (-not ($hashExt.ContainsKey($_.extension)))
	{
		$hashExt.Add($_.extension,$null)
	}
}
$hashExt.Keys | % {
	$Files = Get-ChildItem "\\fileshare\sharename" -Recurse -Filter "*.$($_)"
	>> Blah Blah :) Insert rest here <<
}

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33751326
of course not, just create an array of all the extensions you wish to look for and use for-each to loop through them:


$extensions = "*.txt","*.log"

foreach ($ext in $extensions){
$( $Files = Get-ChildItem "c:\temp\files" -Recurse -Filter $ext
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\outpu.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" ) 
}

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33751341
u can drop the extensions declaration and use the foreach pipe to iterate through them:


"*.txt","*.log" | foreach {
$( $Files = Get-ChildItem "c:\temp\files" -Recurse -Filter $_
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\log.dat
""
"Total Number of Files: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Gb)Gb" ) 
}

Open in new window

0
 

Author Closing Comment

by:tdodd72
ID: 33754740
Many thanks Sedgwick. Once again you have really helped me, thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now