Solved

Displaying of data in powershell

Posted on 2010-09-24
5
837 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

19 Experts available now in Live!

Get 1:1 Help Now