Script to check if PC has an SSD and assign disk to variable for later use

DD90 21 Reputation points
2020-08-26T17:01:47.397+00:00

I'm trying to create a PowerShell script to check if any of the computer's disks is an SSD and then pass that disk number as a variable to other scripts, which will use it to select which disk a Windows image will be installed to. Also, the SSD must be larger than a certain size, otherwise the script may select my UFD boot media. I'm still new to scripting in PS, so I'm having difficulty getting this to work (though I think I'm on the right track). Note that this script needs to run in PowerShell within WinPE.

Here's my pseudo code script:

$disks = Get-PhysicalDisk | Select DeviceID, MediaType, Size

If number of disks > 1 Then
  For Each disk
    If MediaType = SSD AND Size > 100GB Then
      $mainDisk = DeviceID
      Exit Loop
    Else
    End If
  Next disk
Else
End If
If $mainDisk is $null Then
  $mainDisk = disk 0
Else
End If

Pass $mainDisk to script for disk config/Windows install

(Please excuse the lack of proper PS syntax as I figure it would be easier to understand this way due to my limited familiarity with PS syntax)

The key here is the Get-PhysicalDisk Cmdlet. I think a big part of my problem is that the $disks variable is created as a PSCustomObject as opposed to a Hashtable and I can't seem to get the rest of my script to work with it.
If anyone has a better approach, I'd love to hear it.
Any help would be greatly appreciated. Thanks in advance.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,501 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,321 Reputation points
    2020-08-26T18:07:43.353+00:00

    How about this?

    $MainDisk = 0
    ForEach ($disk in (Get-PhysicalDisk | Select-Object DeviceID, MediaType, Size)){
        If ($disk.MediaType -eq 'SSD' -and $disk.Size -gt 100GB){
            $MainDisk = $disk.DeviceID
            break
        }
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.