Solved

Displaying of data in powershell

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

13 Experts available now in Live!

Get 1:1 Help Now