Type Conversion

Hi ,

I am facing the type conversion error in my script

$DiskInfo =get-WmiObject win32_logicaldisk -ComputerName itsusrawsp06501|Where-Object DeviceId -In(“C:”) | Select-object DeviceId,@{n=“Size”;e={[math]::Round($.Size/1GB,2)}},@{n=“FreeSpace”;e={[math]::Round($.FreeSpace/1GB,2)}}

$CDriveSize= $drive.Size
$C_FreeSpacePercent=[Math]::Round(($CFreespace / $CDrive) * 100)

Error : Cannot convert value “C” to type “System.Int32”. Error: “Input string was not in a correct
At G:\SqlScript\powershell\Untitled5.ps1:6 char:9

  • $C_FreeSpacePercent=[Math]::Round(($CFreespace / $CDrive) * 100)
  • CategoryInfo : InvalidArgument: (:slight_smile: [], RuntimeException
  • FullyQualifiedErrorId : InvalidCastFromStringToInteger

can anyone help me sort the issues am facing


I recommend formatting your code by enclosing it using pre /pre. I wasn’t able to replicate your error, however, you are referencing your variables incorrectly. This is the adjusted version:


$DiskInfo =get-WmiObject win32_logicaldisk -ComputerName localhost|Where-Object DeviceId -In(“C:”) | Select-object DeviceId,@{n=“Size”;e={[math]::Round($.Size/1GB,2)}},@{n=“FreeSpace”;e={[math]::Round($.FreeSpace/1GB,2)}}

[int]$CDriveSize= $diskinfo.Size
[int]$CFreeSpace= $diskinfo.FreeSpace
$C_FreeSpacePercent=[Math]::Round(($CFreespace / $CDriveSize) * 100)



Try it again and see if you get the same error.

Use the WMI filter to filter left and only return what you want from Win32_LogicalDisk:

$DiskInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DeviceId = "C:"' | 
            Select-object DeviceId,


As BenT said, it looks like you aren’t referencing your variables correctly. Specifically, in the division on line 6, $CDrive probably should be $CDriveSize.

Here’s an adjusted version that doesn’t change your process but makes it a little cleaner and more flexible:

## input variables
$drive = 'C:'
$computer = "localhost"

## get drive information
$AllDrives = Get-WmiObject win32_logicaldisk -ComputerName "$computer"
$SelectedDrive = $AllDrives | Where-Object DeviceId -In("$drive")
$SelectedDriveData = $SelectedDrive | Select-object DeviceId,@{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

## calculate free space percentage
$SelectedDriveSize = $SelectedDriveData.Size
$SelectedDriveFreeSpace = $SelectedDriveData.FreeSpace
$SelectedDriveFreePercent = [Math]::Round(($SelectedDriveFreeSpace / $SelectedDriveSize) * 100)

## show the result

You can do all of these calculations within calculated expressions without breaking things into separate variables. Additionally, you can use string format to make it a percentage:

$DiskInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DeviceId = "C:"' | 
            Select-object DeviceId,
                          @{Name="UsedPct";Expression={($_.FreeSpace / $_.Size).ToString('P')}},
                          @{Name="UsedPctv2";Expression={'{0:P}' -f ($_.FreeSpace / $_.Size)}}



DeviceId  : C:
Size      : 475.69
FreeSpace : 278.64
UsedPct   : 58.58%
UsedPctv2 : 58.58%