0
# Locate Local HDD drive letter (100GB+)
#(Get-WmiObject -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType LIKE 3" | Sort-Object -Descending Size)[0] | Format-List *
$oLocalLargestHDD = (Get-WmiObject -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType LIKE 3" | Sort-Object -Descending Size)[0]
If ($oLocalLargestHDD.Size -gt 100000000)
{
    $sMsg = "HDD ( $oLocalLargestHDD.Name ) is found!"
    Write-Host $sMsg
} else {
    Write-Host "HDD is NOT found!"
}

outputs:

HDD ( \BRIANG\root\cimv2:Win32_LogicalDisk.DeviceID="C:".Name ) is found!

instead of

HDD ( C: ) is found!

1
  • $sMsg = "HDD $($oLocalLargestHDD.Name) is found!" Commented Sep 10, 2015 at 18:00

1 Answer 1

1

You are not doing variable expansion properly. You need to have a subexpression. You were close.

$sMsg = "HDD ($($oLocalLargestHDD.Name)) is found!"

Or you can use the format operator.

$sMsg = "HDD ({0}) is found!" -f $oLocalLargestHDD.Name

What you were seeing was the string representation of the $oLocalLargestHDD object. Using either of the methods above allow you to use the property.

Sign up to request clarification or add additional context in comments.

4 Comments

Matt, Thank you sir! I prefer the 2nd of your examples.
@BrianGonzalez You are most welcome. If this answered your question consider marking it as so with the checkmark to the left.
Can i use this method when using Write-Host as well? it doesn't appear to like the "-f" format argument.
@BrianGonzalez If you put the whole thing in brackets it will be fine. Write-Host ("HDD ({0}) is found!" -f $oLocalLargestHDD.Name)

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.