Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

How to highlight a cell in Excel a certain color?

$
0
0

I found this code online and it works perfect.    

 

 

$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,5) = “Free Space (%)” 

$c.cells.item(1,6) = "Name "

 

$d = $c.UsedRange 

$d.Interior.ColorIndex = 19 

$d.Font.ColorIndex = 11 

$d.Font.Bold = $True 

$d.EntireColumn.AutoFit()

 

$intRow = 2

 

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

foreach ($strComputer in $colComputers) 

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

foreach ($objdisk in $colDisks) 

$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, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 

$c.cells.item($introw, 6) = $objdisk.volumename

 

$intRow = $intRow + 1 

}

$d.EntireColumn.AutoFit()

cls

 

 

 

ROW 5 in the Excel output spreadsheet is this:

 

$c.Cells.Item(1,5) = “Free Space (%)” 

and near the bottom it is this line that writes the data there:

  $c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 

 

What I want to know how to do is IF "Free Space is 10% or LESS"  THEN make that cell RED 

 

Any help or ideas would be great!!

 

 

 

 

 

 


Viewing all articles
Browse latest Browse all 10624

Trending Articles