Solved

powershell report script by file type

Posted on 2010-09-23
8
2,312 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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