-1

Is there any cmdlet way to get if a Disk is fixed or removable given a code like this?

$disk = Get-Disk -Number 1
Get-DiskDriveType $disk

Where Get-DiskDriveType should return either Removable or Fixed.

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 3
    Check the `DriveType` property of the [`Win32_LogicalDisk`](https://learn.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-logicaldisk) class. – Ansgar Wiechers Jul 21 '18 at 22:02
  • @AnsgarWiechers How do I get it? Get-Disk doesn't provide that, right? – SuperJMN Jul 21 '18 at 22:06
  • 2
    How about you actually *read* the page I linked to? – Ansgar Wiechers Jul 21 '18 at 22:08
  • @AnsgarWiechers OK, my fault. Thanks. Tip for next time you help someone: just add a "look for x on that page". It will add more value to your answers. – SuperJMN Jul 21 '18 at 22:14
  • OK, one more question. How do I query it if all I have is a Disk provided by `Get-Disk`. I mean, wow do I get that property given a `Disk`? Thanks in advance. – SuperJMN Jul 21 '18 at 22:22
  • 3
    `Get-Disk` lists physical disks. Use the `UniqueId` of its output for selecting the corresponding `Win32_DiskDrive` object (property `PNPDeviceId`), then [correlate physical with logical disk](https://stackoverflow.com/a/31092004/1630171). – Ansgar Wiechers Jul 21 '18 at 22:45
  • @AnsgarWiechers I'm sorry, I'm queried my disks and under PNPDevice, they show nothing. Please, take a look => https://i.imgur.com/77yKStX.png Is there a problem? – SuperJMN Jul 21 '18 at 23:31
  • `Win32_DiskDrive`, not `Win32_LogicalDisk`. The latter comes *after* you identified the physical disk in the `Win32_DiskDrive` list. – Ansgar Wiechers Jul 22 '18 at 00:16
  • `Get-Disk` and `Get-CimInstance Win32_DiskDrive` basically provide you with the same information, but you need the latter for getting the associated logical disk, so you need to select the right `Win32_DiskDrive` instance using the information from the `Get-Disk` output. – Ansgar Wiechers Jul 22 '18 at 00:19

3 Answers3

3

Inventory Drive Types by Using PowerShell

https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/10/inventory-drive-types-by-using-powershell

Two methods:

Get-Volume

DriveLetter FileSystemLabel FileSystem  DriveType  HealthStatus SizeRemaining Size
----------- ----------- ----------  ---------  ---------- ----------       ----

C           SSD         NTFS        Fixed      Healthy      75.38 GB  148.53 GB
E           HybridTe... NTFS        Fixed      Healthy     560.71 GB  931.39 GB
D           FourTB_B... NTFS        Fixed      Healthy        1.5 TB    3.64 TB
F           TwoTB_BU... NTFS        Fixed      Healthy     204.34 GB    1.82 TB
G           USB3        NTFS        Removable  Healthy       6.73 GB   58.89 GB
            Recovery    NTFS        Fixed      Healthy      22.96 MB     300 MB
H                                   CD-ROM     Healthy           0 B        0 B

Or

$hash = @{
    2 = "Removable disk"
    3 = "Fixed local disk"
    4 = "Network disk"
    5 = "Compact disk"
}

Get-CimInstance Win32_LogicalDisk |
Select DeviceID, VolumeName,
@{LABEL='TypeDrive';EXPRESSION={$hash.item([int]$_.DriveType)}}
Community
  • 1
  • 1
postanote
  • 15,138
  • 2
  • 14
  • 25
  • Thanks for you answer, but I would like to know if a Disk is removable or not given a DiskNumber. Is that possible? – SuperJMN Jul 25 '18 at 09:55
  • No worries, but as for this … 'or not given a DiskNumber'... You did not ask this in your original post. Yet, the answer is, sure, you just create a list of drives and pipe that to the cmdlet / function or do a regex match. So, what are you really trying to do? Any mounted drive / or drive mountpoint can / will have a drive letter / disk letter - number. – postanote Jul 26 '18 at 00:33
  • I wanted to add (but it's not letting me edit my comment)… Any plugged in non formatted drive will get a DeviceId, but of course no letter or volume info. – postanote Jul 26 '18 at 01:15
1
 Get-Volume | Where-Object {$_.DriveType -eq 'removable'} | Get-Partition | Get-Disk | Where-Object {$_.Number -eq $diskNumber}
Manuel Amstutz
  • 1,311
  • 13
  • 33
0

The Bustype property returned by Get-Disk was good for my needs. I didn't have volumes on my disks yet so the Get-Volume wouldn't work for me. This is what I used:

$USBDisks = Get-Disk | Where-Object -FilterScript {$_.Bustype -eq "USB"}
Jroonk
  • 1,376
  • 1
  • 9
  • 12