Attempted to divide by zero

JoeyD 1 Reputation point

any idea why I am getting this error



$report += Get-WmiObject Win32_Volume -ComputerName $VM| ForEach {
New-Object PSObject -Property @{
Computername = $VM
Name = $.Name
Label = $
FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
UsedSpace_GB = ([Math]::Round($.Capacity /1GB,2)) - ([Math]::Round($.FreeSpace /1GB,2))
Percent_Free = (int)
filesystem = $_.filesystem


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,895 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 25,671 Reputation points

    It would appear that this is the only statement that could generate that error.

    Percent_Free = ([int]($.Freespace*100/$_.Capacity))

    So you have some volume where $_.Capacity is zero.

    Add some debugging statements to your script to determine which volume is causing the problem. Maybe start by just looking at the raw freespace and capacity numbers

    Add an if statement to test for zero and don't do the division if it is.

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 19,141 Reputation points Microsoft Vendor


    The error message means the $_.Capacity of some volume is zero. It could be the CD-ROM. If you only want to get the hard drives you can filter with DriveType like below

    $report += Get-WmiObject Win32_Volume -ComputerName $VM | Where-Object {$_.DriveType -eq 3} | ForEach{ ... }  


    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments