# Type Conversion

**URL:** <https://forums.powershell.org/t/type-conversion/13562>\
**Category:** PowerShell Help\
**Created:** [December 18, 2019, 11:18am UTC](https://forums.powershell.org/t/type-conversion/13562 "2019-12-18T11:18:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohankumars](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@mohankumars](https://forums.powershell.org/u/mohankumars)\
**Post date:** [December 18, 2019, 11:18am UTC](https://forums.powershell.org/t/type-conversion/13562/1 "2019-12-18T11:18:25Z")

</div>

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  
$CFreeSpace=$drive.FreeSpace  
$C\_FreeSpacePercent=[Math]::Round(($CFreespace / $CDrive) \* 100)  
$C\_FreeSpacePercent

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

- $C\_FreeSpacePercent=[Math]::Round(($CFreespace / $CDrive) \* 100)
- 

```auto

```

- CategoryInfo : InvalidArgument: (🙂 , RuntimeException
- FullyQualifiedErrorId : InvalidCastFromStringToInteger

can anyone help me sort the issues am facing

&nbsp;

---

<div class="post-metadata">

**Author:** ![dasgerman](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dasgerman](https://forums.powershell.org/u/dasgerman)\
**Post date:** [December 18, 2019, 11:54am UTC](https://forums.powershell.org/t/type-conversion/13562/2 "2019-12-18T11:54:58Z")

</div>

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:

[pre]

$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)  
$C\_FreeSpacePercent

[/pre]

&nbsp;

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 18, 2019, 12:08pm UTC](https://forums.powershell.org/t/type-conversion/13562/3 "2019-12-18T12:08:09Z")

</div>

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,
                          @{Name="Size";Expression={[math]::Round($_.Size/1GB,2)}},
                          @{Name="FreeSpace";Expression={[math]::Round($_.FreeSpace/1GB,2)}}

$DiskInfo
```

---

<div class="post-metadata">

**Author:** ![grokkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grokkit/32/970_2.png) [@grokkit](https://forums.powershell.org/u/grokkit)\
**Post date:** [December 18, 2019, 12:25pm UTC](https://forums.powershell.org/t/type-conversion/13562/4 "2019-12-18T12:25:31Z")

</div>

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

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
$SelectedDriveFreePercent
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 18, 2019, 12:46pm UTC](https://forums.powershell.org/t/type-conversion/13562/5 "2019-12-18T12:46:36Z")

</div>

You can do all of these calculations within calculated expressions without breaking things into separate variables. Additionally, you can use [string format](https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings) to make it a percentage:

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

$DiskInfo
```

Output:

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

---

<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:31pm UTC](https://forums.powershell.org/t/type-conversion/13562/6 "2024-05-16T20:31:14Z")

</div>

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


