Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

powershell report script by file type

Posted on 2010-09-23
8
Medium Priority
?
2,450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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