Solved

powershell report script by file type

Posted on 2010-09-23
8
2,290 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 17

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

930 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

12 Experts available now in Live!

Get 1:1 Help Now