# Error in script : Attempted to divide by zero.

**URL:** <https://forums.powershell.org/t/error-in-script-attempted-to-divide-by-zero/12524>\
**Category:** PowerShell Help\
**Created:** [May 9, 2019, 10:27am UTC](https://forums.powershell.org/t/error-in-script-attempted-to-divide-by-zero/12524 "2019-05-09T10:27:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![docrst](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@docrst](https://forums.powershell.org/u/docrst)\
**Post date:** [May 9, 2019, 10:27am UTC](https://forums.powershell.org/t/error-in-script-attempted-to-divide-by-zero/12524/1 "2019-05-09T10:27:40Z")

</div>

I am getting an error in my script for getting drives under 10% free space. on this line

```
gwmi win32_logicaldisk -ComputerName $args | Where-Object {($_.freespace/$_.size) -lt '0.10'} | ft SystemName,VolumeName
```

> \<p style="font-weight: 400;"\>Attempted to divide by zero.\</p\> \<p style="font-weight: 400;"\>At C:\Users\robecow\Desktop\less10.ps1:42 char:61\</p\> \<p style="font-weight: 400;"\>+ ... rName $args | Where-Object {($\_.freespace/$\_.size) -lt '0.10'} | ft S ...\</p\> \<p style="font-weight: 400;"\>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \</p\> \<p style="font-weight: 400;"\> + CategoryInfo : NotSpecified: (:) [], RuntimeException\</p\> \<p style="font-weight: 400;"\> + FullyQualifiedErrorId : RuntimeException\</p\> ##============================================================================== ## START ##============================================================================== #\>

```
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
#$cre = get-Credential # This user account should be Adminstrator
$file = get-Content G:\Desktop\PS\list.txt # Replace it with your TXT file which contain Name of Computers
$OS = (gwmi win32_operatingsystem).caption
$MediaType = gwmi -Class MSFT_PhysicalDisk -Namespace root\Microsoft\Windows\Storage | Select FriendlyName, MediaType
$LogicalDisks = gwmi Win32_LogicalDisk
$LocalHDisks = $LogicalDisks | Where-Object { $_.DriveType -eq 3 }
$RemoteDrives = gwmi Win32_LogicalDisk | Where-Object { $_.DriveType -eq 4 }
$OpticalDrives = gwmi Win32_CDRomDrive
$Records = 1000

ForEach ( $args in $file) {  
gwmi win32\_logicaldisk -ComputerName $args | Where-Object {($_.freespace/$_.size) -lt ‘0.10’} | ft SystemName,VolumeName,  
@{n=“DriveLetter”;e={$_.DeviceID}}, `  
@{n=“Size(mb)”;e={[decimal](“{0:N0}” -f($_.size/1mb))}}, `  
@{n=“Free Space(mb)”;e={[decimal](“{0:N0}” -f($_.freespace/1mb))}} , `  
@{n=“Free (%)”;e={“{0,6:P0}” -f(($_.freespace/1mb) / ($_.size/1mb))}}, `  
@{n=“Drive Type”;e={switch ($_.DriveType) { 0 {“(unknown)”} 2 {“(USB/FD)”} 3 {“HDD”} 4 {“SSD”} 5 {“CD/DVD/BD”} 6 {“RAM disk”}}}}, `  
@{n=“Operating System”;e={$OS}}  
}

# Show Progress Bar - START

for($i = 1; $i -lt 101; $i++ ) {write-progress Updating progress-\> -perc $i} for($i = 1; $i -lt 101; $i++ ) { write-progress Updating progress -percentcomplete $i -id 1}

# Show Progress Bar - END

\<#

# Setup email parameters

$subject = "CIs with less than 10% freespace Report - " + $today  
$body = “The Reports Completed at:$(((get-date).ToUniversalTime()).ToString(“yyyy-MM-dd”)).”  
$priority = “Normal”  
$smtpServer = " "  
$emailFrom = “[robecow@cdw.com](mailto:robecow@cdw.com)”  
$emailTo = “[robecow@cdw.com](mailto:robecow@cdw.com)”;p

# Setup email parameters

# Send the Reports email

Send-MailMessage -To $emailTo -Subject $subject -Body $body -SmtpServer $smtpServer -From $emailFrom -Priority $priority  
Write-Host “Email sent to: $emailTo at:$(((get-date).ToUniversalTime()).ToString(“yyyy-MM-dd”))”.

# Send the Reports email

#\>

#---------- Clean up and exit.  
Write-Host “Cleanup and Exit at:$(((get-date).ToUniversalTime()).ToString(“yyyy-MM-dd”))”.  
exit  
#---------- Clean up and exit.

##==============================================================================

## End of Script

##==============================================================================
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 9, 2019, 12:49pm UTC](https://forums.powershell.org/t/error-in-script-attempted-to-divide-by-zero/12524/2 "2019-05-09T12:49:38Z")

</div>

It seems like your variable $\_.size is empty … you might check if the cmdlet at the pipeline position before returns what you expect.

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [May 9, 2019, 3:02pm UTC](https://forums.powershell.org/t/error-in-script-attempted-to-divide-by-zero/12524/3 "2019-05-09T15:02:46Z")

</div>

Win32\_LogicalDisk also returns disks with no size if you have, for example, a CD drive or something. You need to make sure you account for that.

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/error-in-script-attempted-to-divide-by-zero/12524/4 "2024-05-16T20:32:49Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


