Solved

Displaying of data in powershell

Posted on 2010-09-24
5
852 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
[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
  • 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

691 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