title: Powershell disk space script
body: <p><a href="http://gallery.technet.microsoft.com/scriptcenter/6e935887-6b30-4654-b977-6f5d289f3a63#content" rel="nofollow">I have a powershell script</a>. In short, the script can collect disk space info from multiple servers and put that in a .htm report that can be sent by mail.</p>
  
  <p>Now, the script does collect the disk info into a report, but i would like it to send an email only if one of the disks has free space lower than the critical value defined in the script.</p>
  
  <p>The script:</p>
  
  <pre><code># First lets create a text file, where we will later save the freedisk space info 
  $freeSpaceFileName = "c:\disk_space.htm" 
  $serverlist = "c:\sl.txt" 
  $warning = 50 
  $critical = 10 
  New-Item $freeSpaceFileName -ItemType file -Force 
  # Getting the freespace info using WMI 
  #Get-WmiObject win32_logicaldisk  | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File "C:\temp\FreeSpace.txt 
  # Function to write the HTML Header to the file 
  Function writeHtmlHeader 
  { 
  param($fileName) 
  $date = ( get-date ).ToString('dd.MM.yyyy - HH:mm') 
  Add-Content $fileName "&lt;html&gt;" 
  Add-Content $fileName "&lt;head&gt;" 
  Add-Content $fileName "&lt;meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'&gt;" 
  Add-Content $fileName '&lt;title&gt;Disk Report&lt;/title&gt;' 
  add-content $fileName '&lt;STYLE TYPE="text/css"&gt;' 
  add-content $fileName  "&lt;!--" 
  add-content $fileName  "td {" 
  add-content $fileName  "font-family: Tahoma;" 
  add-content $fileName  "font-size: 11px;" 
  add-content $fileName  "border-top: 1px solid #999999;" 
  add-content $fileName  "border-right: 1px solid #999999;" 
  add-content $fileName  "border-bottom: 1px solid #999999;" 
  add-content $fileName  "border-left: 1px solid #999999;" 
  add-content $fileName  "padding-top: 0px;" 
  add-content $fileName  "padding-right: 0px;" 
  add-content $fileName  "padding-bottom: 0px;" 
  add-content $fileName  "padding-left: 0px;" 
  add-content $fileName  "}" 
  add-content $fileName  "body {" 
  add-content $fileName  "margin-left: 5px;" 
  add-content $fileName  "margin-top: 5px;" 
  add-content $fileName  "margin-right: 0px;" 
  add-content $fileName  "margin-bottom: 10px;" 
  add-content $fileName  "" 
  add-content $fileName  "table {" 
  add-content $fileName  "border: thin solid #000000;" 
  add-content $fileName  "}" 
  add-content $fileName  "--&gt;" 
  add-content $fileName  "&lt;/style&gt;" 
  Add-Content $fileName "&lt;/head&gt;" 
  Add-Content $fileName "&lt;body&gt;" 
  
  add-content $fileName  "&lt;table width='100%'&gt;" 
  add-content $fileName  "&lt;tr bgcolor='#CCCCCC'&gt;" 
  add-content $fileName  "&lt;td colspan='7' height='25' align='center'&gt;" 
  add-content $fileName  "&lt;font face='tahoma' color='#003399' size='4'&gt;&lt;strong&gt;Disk Report - $date&lt;/strong&gt;&lt;/font&gt;" 
  add-content $fileName  "&lt;/td&gt;" 
  add-content $fileName  "&lt;/tr&gt;" 
  add-content $fileName  "&lt;/table&gt;" 
  
  } 
  
  # Function to write the HTML Header to the file 
  Function writeTableHeader 
  { 
  param($fileName) 
  
  Add-Content $fileName "&lt;tr bgcolor=#CCCCCC&gt;" 
  Add-Content $fileName "&lt;td width='10%' align='center'&gt;Drive&lt;/td&gt;" 
  Add-Content $fileName "&lt;td width='50%' align='center'&gt;Drive Label&lt;/td&gt;" 
  Add-Content $fileName "&lt;td width='10%' align='center'&gt;Total Capacity(GB)&lt;/td&gt;" 
  Add-Content $fileName "&lt;td width='10%' align='center'&gt;Used Capacity(GB)&lt;/td&gt;" 
  Add-Content $fileName "&lt;td width='10%' align='center'&gt;Free Space(GB)&lt;/td&gt;" 
  Add-Content $fileName "&lt;td width='10%' align='center'&gt;Freespace %&lt;/td&gt;" 
  Add-Content $fileName "&lt;/tr&gt;" 
  } 
  
  Function writeHtmlFooter 
  { 
  param($fileName) 
  
  Add-Content $fileName "&lt;/body&gt;" 
  Add-Content $fileName "&lt;/html&gt;" 
  } 
  
  Function writeDiskInfo 
  { 
  param($fileName,$devId,$volName,$frSpace,$totSpace) 
  $totSpace=[math]::Round(($totSpace/1073741824),2) 
  $frSpace=[Math]::Round(($frSpace/1073741824),2) 
  $usedSpace = $totSpace - $frspace 
  $usedSpace=[Math]::Round($usedSpace,2) 
  $freePercent = ($frspace/$totSpace)*100 
  $freePercent = [Math]::Round($freePercent,0) 
   if ($freePercent -gt $warning) 
   { 
   Add-Content $fileName "&lt;tr&gt;" 
   Add-Content $fileName "&lt;td&gt;$devid&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$volName&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$totSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$usedSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$frSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td bgcolor='#32B232' align=center&gt;$freePercent&lt;/td&gt;"
   Add-Content $fileName "&lt;/tr&gt;" 
   } 
   elseif ($freePercent -le $critical) 
   { 
   Add-Content $fileName "&lt;tr&gt;" 
   Add-Content $fileName "&lt;td&gt;$devid&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$volName&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$totSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$usedSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$frSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td bgcolor='#FF0000' align=center&gt;$freePercent&lt;/td&gt;"
   Add-Content $fileName "&lt;/tr&gt;" 
   } 
   else 
   { 
   Add-Content $fileName "&lt;tr&gt;" 
   Add-Content $fileName "&lt;td&gt;$devid&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$volName&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$totSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$usedSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td&gt;$frSpace&lt;/td&gt;" 
   Add-Content $fileName "&lt;td bgcolor='#E1E100' align=center&gt;$freePercent&lt;/td&gt;"
   Add-Content $fileName "&lt;/tr&gt;" 
   } 
  } 
  Function sendEmail 
  { param($from,$to,$subject,$smtphost,$htmlFileName) 
  $body = Get-Content $htmlFileName 
  $smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
  $msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body 
  $msg.isBodyhtml = $true 
  $smtp.send($msg) 
  
  } 
  
  writeHtmlHeader $freeSpaceFileName 
  foreach ($server in Get-Content $serverlist) 
  { 
   Add-Content $freeSpaceFileName "&lt;table width='100%'&gt;&lt;tbody&gt;" 
   Add-Content $freeSpaceFileName "&lt;tr bgcolor='#CCCCCC'&gt;" 
   Add-Content $freeSpaceFileName "&lt;td width='100%' align='center' colSpan=6&gt;&lt;font face='tahoma' color='#003399' size='2'&gt;&lt;strong&gt; $server &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;" 
   Add-Content $freeSpaceFileName "&lt;/tr&gt;" 
  
   writeTableHeader $freeSpaceFileName 
  
   $dp = Get-WmiObject win32_logicaldisk -ComputerName $server |  Where-Object {$_.drivetype -eq 3} 
   foreach ($item in $dp) 
   { 
   Write-Host  $item.DeviceID  $item.VolumeName $item.FreeSpace $item.Size 
   writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size 
  
   } 
  } 
  writeHtmlFooter $freeSpaceFileName 
  $date = ( get-date ).ToString('dd.MM.yyyy') 
  
  #sendEmail cdarwin@evolve.com cdarwin@evolve.com "Disk Space Report - $Date" hub1 $freeSpaceFileName 
  </code></pre>
  
  <p>I already tried to add some kind of IF, but it didn't work,</p>
  
  <p>Any help is appreciated.</p>