Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2494
  • Last Modified:

powershell report script by file type

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
tdodd72
Asked:
tdodd72
  • 4
  • 3
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
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
 
tdodd72Author Commented:
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
 
tdodd72Author Commented:
I tried putting ) and } at the end but for some reason still cannot get this to run. I am running powershell 2.0
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
tdodd72Author Commented:
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
 
LearnctxEngineerCommented:
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
 
Meir RivkinFull stack Software EngineerCommented:
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
 
Meir RivkinFull stack Software EngineerCommented:
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
 
tdodd72Author Commented:
Many thanks Sedgwick. Once again you have really helped me, thanks.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now