Unable to fix the issue with powershell script when using get-disk command

Mathivanan, Lakshmi priya 21 Reputation points
2021-11-30T05:24:22.72+00:00

Get-disk -Number 3 | select UniqueId

gives below error

Get-Disk : No MSFT_Disk objects found with property 'Number' equal to '3'. Verify the value of the property and retry.
At line:1 char:13

  • $UNIQUEID = Get-Disk -Number 3 | Select UniqueId
  • ~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ObjectNotFound: (3:UInt32) [Get-Disk], CimJobException
  • FullyQualifiedErrorId : CmdletizationQuery_NotFound_Number,Get-Disk
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Clément BETACORNE 2,496 Reputation points
    2021-11-30T08:19:30.09+00:00

    Hello,

    Are you sure that you have 4 disks attached on your computer ?
    If it is not the case you should use wrap your command with a try catch in order to prevent the message for example :

    try {
    Get-disk -number 3 -ErrorAction Stop
    }
    catch {
    Write-output "Less than 4 disk"
    }
    

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-11-30T16:01:45.277+00:00

    You can also code it this way (although the code submitted by @Clément BETACORNE will work quite well).

    $d = Get-Disk |  
            ForEach-Object{  
                if ($_.Number -eq $WantedDiskNumber){  
                    $_.UniqueId  
                }  
            }  
    if ($d){  
        $d  
    }  
    else{  
        "Disk number $WantedDiskNumber wasn't found"  
    }  
    
    0 comments No comments

  2. Mathivanan, Lakshmi priya 21 Reputation points
    2021-12-02T04:04:16.977+00:00

    I can able to get the output for disk number 0 but I couldn't get it for number 3. and I have the disk installed.

    can someone please help me to troubleshoot this issue?


  3. Mathivanan, Lakshmi priya 21 Reputation points
    2021-12-03T03:47:21.593+00:00

    154665-img.png

    0 comments No comments

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.