Solved

PowerShell Script to list drive free space in MS Excel

Posted on 2014-04-07
7
825 Views
Last Modified: 2014-04-08
I am using the following PowerShell script to list down the percentage free space of disks for C: and E: drives only. But the problem is that E: drives are coming just on the next row of C: drive.

OutputBut I want the output like following so that both C: and E: drives come under same row and the blank cells will be colored in Red.

Please help me to do that.

Desired Output
0
Comment
Question by:hchabria
[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
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39982836
the following PowerShell script
0
 

Author Comment

by:hchabria
ID: 39982868
$erroractionpreference = “SilentlyContinue” 
$a = New-Object -comobject Excel.Application 
$a.visible = $True

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = “Machine Name” 
$c.Cells.Item(1,2) = “Drive” 
#$c.Cells.Item(1,3) = “Total size (GB)” 
#$c.Cells.Item(1,4) = “Free Space (GB)” 
$c.Cells.Item(1,3) = “Free Space (%)” 
#$c.cells.item(1,6) = "Name "
$c.Cells.Item(1,4) = “Drive” 
$c.Cells.Item(1,5) = "Free Space (%)"

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 
$d.EntireColumn.AutoFit()

$intRow = 2
$intRow1 = 1

$colComputers = get-content "C:\MorningCheck\Servers.txt"

foreach ($strComputer in $colComputers) 
{

$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3"

foreach ($objdisk in $colDisks) 
{ 
	if($objdisk.DeviceID -eq "C:")
	{
		$c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
		$c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
#		$c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB) 
#		$c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB) 
		$c.Cells.Item($intRow, 3) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
#		$c.cells.item($introw, 6) = $objdisk.volumename

		$intRow = $intRow + 1 
	} 
		
	if($objdisk.DeviceID -eq "E:")
	{
#		$c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
		$c.Cells.Item($intRow, 4) = $objDisk.DeviceID 
#		$c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB) 
#		$c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB) 
		$c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
#		$c.cells.item($introw, 6) = $objdisk.volumenam

		$intRow = $intRow + 1 
	} 
}
$d.EntireColumn.AutoFit()
}
cls

Open in new window

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39982891
You're skipping to the next row after C and E.
Just increment the row after both tests: remove lines 44 and 56.
You might end up with blank rows, but you can easily filter them after.
$erroractionpreference = “SilentlyContinue” 
$a = New-Object -comobject Excel.Application 
$a.visible = $True

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = “Machine Name” 
$c.Cells.Item(1,2) = “Drive” 
#$c.Cells.Item(1,3) = “Total size (GB)” 
#$c.Cells.Item(1,4) = “Free Space (GB)” 
$c.Cells.Item(1,3) = “Free Space (%)” 
#$c.cells.item(1,6) = "Name "
$c.Cells.Item(1,4) = “Drive” 
$c.Cells.Item(1,5) = "Free Space (%)"

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 
$d.EntireColumn.AutoFit()

$intRow = 2
$intRow1 = 1

$colComputers = get-content "C:\MorningCheck\Servers.txt"

foreach ($strComputer in $colComputers) 
{

$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3"

foreach ($objdisk in $colDisks) 
{ 
	if($objdisk.DeviceID -eq "C:")
	{
		$c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
		$c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
#		$c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB) 
#		$c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB) 
		$c.Cells.Item($intRow, 3) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
#		$c.cells.item($introw, 6) = $objdisk.volumename

	} 
		
	if($objdisk.DeviceID -eq "E:")
	{
#		$c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
		$c.Cells.Item($intRow, 4) = $objDisk.DeviceID 
#		$c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB) 
#		$c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB) 
		$c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
#		$c.cells.item($introw, 6) = $objdisk.volumenam

	} 
	$intRow = $intRow + 1 
}
$d.EntireColumn.AutoFit()
}
cls

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39982905
Try this:   (updated)
$erroractionpreference = “SilentlyContinue”
$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = “Machine Name”
$c.Cells.Item(1,2) = “Drive”
#$c.Cells.Item(1,3) = “Total size (GB)”
#$c.Cells.Item(1,4) = “Free Space (GB)”
$c.Cells.Item(1,3) = “Free Space (%)”
#$c.cells.item(1,6) = "Name "
$c.Cells.Item(1,4) = “Drive”
$c.Cells.Item(1,5) = "Free Space (%)"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit()

$intRow = 2
$intRow1 = 1

$colComputers = get-content "C:\MorningCheck\Servers.txt"

foreach ($strComputer in $colComputers)
{

$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3"

foreach ($objdisk in $colDisks)
{
      if($objdisk.DeviceID -eq "C:")
      {
            $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
            $c.Cells.Item($intRow, 2) = $objDisk.DeviceID
#            $c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB)
#            $c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB)
            $c.Cells.Item($intRow, 3) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
#            $c.cells.item($introw, 6) = $objdisk.volumename

      }
            
      if($objdisk.DeviceID -eq "E:")
      {
#            $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
            $c.Cells.Item($intRow, 4) = $objDisk.DeviceID
#            $c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB)
#            $c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB)
            $c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
#            $c.cells.item($introw, 6) = $objdisk.volumenam
      }
    else
    {
      $c.Cells.Item($intRow, 4).Interior.ColorIndex = 3
      $c.Cells.Item($intRow, 5).Interior.ColorIndex = 3
    }
            


}
$intRow = $intRow + 1
$d.EntireColumn.AutoFit()
}
cls
0
 

Author Comment

by:hchabria
ID: 39983013
@Raheman-
The script is working fine except D and E columns in Excel are coming under red.
Output
0
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39983049
Updated and the new code is:
#---------------------------------------
$erroractionpreference = “SilentlyContinue”
$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = “Machine Name”
$c.Cells.Item(1,2) = “Drive”
#$c.Cells.Item(1,3) = “Total size (GB)”
#$c.Cells.Item(1,4) = “Free Space (GB)”
$c.Cells.Item(1,3) = “Free Space (%)”
#$c.cells.item(1,6) = "Name "
$c.Cells.Item(1,4) = “Drive”
$c.Cells.Item(1,5) = "Free Space (%)"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit()

$intRow = 2
$intRow1 = 1

$colComputers = get-content "C:\MorningCheck\Servers.txt"

foreach ($strComputer in $colComputers)
{

$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3"
$found=0
foreach ($objdisk in $colDisks)
{
      if($objdisk.DeviceID -eq "C:")
      {
            $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
            $c.Cells.Item($intRow, 2) = $objDisk.DeviceID
#            $c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB)
#            $c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB)
            $c.Cells.Item($intRow, 3) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
#            $c.cells.item($introw, 6) = $objdisk.volumename

      }
   else {        
      if($objdisk.DeviceID -eq "e:")
      {
#            $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
            $c.Cells.Item($intRow, 4) = $objDisk.DeviceID
#            $c.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDisk.Size/1GB)
#            $c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB)
            $c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
#            $c.cells.item($introw, 6) = $objdisk.volumenam
      # $c.Cells.Item($intRow, 4).Interior.ColorIndex = 1
      # $c.Cells.Item($intRow, 5).Interior.ColorIndex = 1
      $found=1
      }
   
  }
 
}
if(!$found)
    {
      $c.Cells.Item($intRow, 4).Interior.ColorIndex = 3
      $c.Cells.Item($intRow, 5).Interior.ColorIndex = 3
    }
$intRow = $intRow + 1
$d.EntireColumn.AutoFit()
}
cls
0
 

Author Closing Comment

by:hchabria
ID: 39985865
Awesome! Thank you so much. It's working fine now as expected.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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