Reputation: 1
i'm a newbie on powershell and i have come up with the problem in variable assignment:
Below is the script is used to retrieve free space percentage for drive with drivetype equal 3
I can't pass $line
to line 6 by retrieving the value from the text file but if I do c:
instead the script works perfectly, what's my mistake?
Get-WmiObject -Class Win32_logicalDisk | ? {$_.DriveType -eq '3'} | select deviceid > driveletter.txt
$content = Get-Content driveletter.txt | select-string ':' -simplematch
foreach ($line in $content)
{
"line $line"
$freespace = Get-WmiObject -Class Win32_logicalDisk | ? {$_.DeviceID -eq '**$line**'}
$drive = ($freeSpace.DeviceID).Split("=")
"Your $drive Free Space Percentage is {0:P2}" -f ($freespace.FreeSpace / $freespace.Size)
}
Upvotes: 0
Views: 472
Reputation: 114
That can be done a little bit more efficiently i think
Get-WmiObject -Class Win32_logicalDisk | Where-Object {$_.DriveType -eq '3'} | ForEach-Object {Write-Host "Your $($_.DeviceID) Free Space Percentage is $($($_.FreeSpace) / $($_.Size))"}
The percentages now show up as 0.xx which isn't ideal. Still looking into that.
**Edit:**Your calculation was a bit off and also did some changes on the write host to make it more readable:
Get-WmiObject -Class Win32_logicalDisk | Where-Object {$_.DriveType -eq '3'} | ForEach-Object {Write-Host ('Your ' + $_.DeviceID + ' Free Space Percentage is ' + ($_.FreeSpace / ($_.Size / 100)))}
Upvotes: 1