Solved

Displaying of data in powershell

Posted on 2010-09-24
5
843 Views
Last Modified: 2012-05-10
Ok guys I have this script here where I could use a little bit of help. This script produces output like this which is great with the exception of a couple of things EX:

Total Number of Word Perfect Documents: 62
Total Size: 54.3721075057983Mb

It would be nice to just display 54MB and not the numbers behind it. Also, I am having trouble getting this to go into an excel spread sheet. I found a couple of tutorials on line and they are either very advance with little documentation or too simplistic for me to implement here.I would like to have the following two columns

Total number of xxx documents        Total size






# Name of script masterfiletype
# The purpose of this script is to find common file extension, display the total amount along with total disk usage
# Written by TDodd


# email pst files


$extensions = "*.pst"

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


# Microsoft word documents


$extensions = "*.doc", "*.docx", "*.rtf"

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

# Power point files

$extensions = "*.ppt", "*.pptx"

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


# Checks for Microsoft project files

$extensions = "*.mpp"

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

# Checks for PDF files adobe files


$extensions = "*.pdf"

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

# checks or compressed files

$extensions = "*.zip", "*.rar"

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

# executibles checks for exectuble files

$extensions = "*.exe"

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

# image files Checks for image files

$extensions = "*.jpg", "*.gif", "*.bmp", "*.tif"

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

# video files Checks for video files

$extensions = "*.mov", "*.wmv", "*.avi", "*.mpeg"

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


# excel documents checks for xls, xlsx and comma seperated value files

$extensions = "*.xls", "*.xlsx", "*.csv"

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



#Word perfect documents Checks for wpd extension

$extensions = "*.wpd"

foreach ($ext in $extensions){
$( $Files = Get-ChildItem "\\fileserver\shares" -Recurse -Filter $ext
$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\output.dat
""
"Total Number of Word perfct Documents: $(([Array]$Files).Count)"
"Total Size: $(($Files | Measure-Object -Sum Length).Sum/1Mb)Mb" )
}
0
Comment
Question by:tdodd72
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:GusGallows
ID: 33757090
Try the following. This will convert the filesize to an integer (so it rounds accordingly) and will output it to a tab delimited file which can be opened in excel and will have lined up columns.

$extensions = @("*.*")

#create output file
$out = "c:\temp\results.txt"
#Write header for output file using `t as the seperator (tab character) 
#I find that tabs are a lot better to work with than commas

$inp = "Total number of $extensions documents`tTotal size"
Out-File $out -InputObject $inp

foreach ($ext in $extensions)
{
	$( $Files = Get-ChildItem ""\\fileserver\shares" -Recurse -Filter $ext
	$Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb" } >> c:\temp\output.dat
	""
	#Write the results to a variable so you can easily manipulate it and put it in a tabbed document
	$totnum = "$(([Array]$Files).Count)"
	$totsize = "$(($Files | Measure-Object -Sum Length).Sum/1Gb)" )
	
	#convert it to an integer so it drops everything after the decimal
	$totsize = [int]$totsize
	#Convert it back to a string so you can append the Gb to it.
	$totsize = [string]$totsize + "Gb"
	
	#Output it to the screen
	$totnum
	$totsize
	
	#Output it to the tab delimited document 
	#(make sure to do an append so it does not write over the doc file.)
	$inp = "$totnum`t$totsize"
	out-file $out -inputobject $inp -append
}

Open in new window

0
 
LVL 12

Expert Comment

by:GusGallows
ID: 33757121
I added the @() to the $extensions so it knows it is an array. If you only have one item in it, sometimes, powershell doesn't treat it right unless it is set up as an array to begin with. (It could break the foreach loop). Also, don't forget to put your own extensions in there. I left *.* because that is what I was testing with to get a big enough selection of files. ;)
0
 

Author Comment

by:tdodd72
ID: 33759201
when running the script I get the following below


The string starting:
At line:32 char:28
+       $inp = "$totnum `t$totsize <<<< "
is missing the terminator: ".
At line:35 char:1

Missing closing ')' in subexpression.
At line:35 char:1

Missing closing '}' in statement block.
At line:35 char:1

______________________________________________________________________________________

Here is what I am  running  changing only the \\servername\shares

xtensions = @("*.mp3", "*.rar", "*.avi")


#create output file
$out = "c:\temp\results.txt"
#Write header for output file using `t as the seperator (tab character)
#I find that tabs are a lot better to work with than commas

$inp = "Total number of $extensions documents`tTotal size"
Out-File $out -InputObject $inp

foreach ($ext in $extensions)
{
      $( $Files = Get-ChildItem ""\\fileshare\share" -Recurse -Filter $ext
      $Files | %{ "$($_.FullName) - $($_.Length/1Kb)Kb"  } >> c:\temp\output.dat
      ""
      #Write the results to a variable so you can easily manipulate it and put it in a tabbed document
      $totnum = "$(([Array]$Files).Count)"
      $totsize = "$(($Files | Measure-Object -Sum Length).Sum/1Gb)" )
      
      #convert it to an integer so it drops everything after the decimal
      $totsize = [int]$totsize
      #Convert it back to a string so you can append the Gb to it.
      $totsize = [string]$totsize + "Gb"
      
      #Output it to the screen
      $totnum
      $totsize
      
      #Output it to the tab delimited document
      #(make sure to do an append so it does not write over the doc file.)
      $inp = "$totnum `t$totsize"
      out-file $out -inputobject $inp -append
}

0
 
LVL 12

Accepted Solution

by:
GusGallows earned 125 total points
ID: 33759240
You have an extra " on the filepath. Remove the one before \\fileshare\share and it should work for you.
0
 

Author Closing Comment

by:tdodd72
ID: 33842559
.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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