• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

Displaying of data in powershell

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
tdodd72
Asked:
tdodd72
  • 3
  • 2
1 Solution
 
GusGallowsCommented:
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
 
GusGallowsCommented:
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
 
tdodd72Author Commented:
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
 
GusGallowsCommented:
You have an extra " on the filepath. Remove the one before \\fileshare\share and it should work for you.
0
 
tdodd72Author Commented:
.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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