Solved

PowerShell Script to list drive free space in MS Excel

Posted on 2014-04-07
7
794 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 34

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 34

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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