0

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)

   }

1 Answer 1

1

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)))}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.