Store inside a variable Value of "DiskNumber"

Hi all!

I need Store inside a variable “DiskNumber” (of get-disk) where FriendlyName (of get-volume) equals “XXXXXX”

I tryed:
$DiskNumber = Get-Disk | Where-Object {$_.FriendlyName -Eq “XXXXXX”}
$DiskNumber = $DiskNumber.Number
Get-Partition -DiskNumber $DiskNumber | Set-Partition - newDriveLetter x

but variable is null!!

Could you help me please?

Thank You very much!

Welcome to the forum. :wave:t4:

You dont need an extra variable definition and re-using the same variable name always looks quirky to me.

You can use a subexpression with the dot notation to access the desired property directly.

$DiskNumber = Get-Disk | Where-Object { $_.FriendlyName -eq 'XXXXX' }
Get-Partition -DiskNumber $($DiskNumber.Number) | Set-Partition - newDriveLetter x

Please when you post code format it as code using the preformatted text button ( </> ). Simply place the cursor on an empty line, click the button and paste your code.

Thanks in advance.

Thanks Olaf for the reply!! I tried but it keeps giving me error, I post it:

$DiskNumber = Get-Disk | Where-Object { $_.FriendlyName -eq 'XXXXX' }
Get-Partition -DiskNumber $($DiskNumber.Number) | Set-Partition - newDriveLetter b
Get-Partition:  Could not validate argument on parameter 'DiskNumber'. The argument is null. Provide value valid for the argument and reissue the command.
In riga:1 car:27
+ Get-Partition -DiskNumber $($DiskNumber.Number) | Set-Partition - new ...
+                           ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Partition], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Partition

if I execute the command Get-Volume the disk with FriendlyName “XXXXX” is seen:

DriveLetter FriendlyName             FileSystemType DriveType HealthStatus OperationalStatus SizeRemaining      Size
----------- ------------             -------------- --------- ------------ ----------------- -------------      ----
            XXXXX                    NTFS           Fixed     Healthy      OK                    262.96 GB 931.51 GB

What output do you get when you run:

Get-Disk | Where-Object { $_.FriendlyName -eq 'XXXXX' }



Windows PowerShell
Copyright (C) Microsoft Corporation. Tutti i diritti riservati.

Prova la nuova PowerShell multipiattaforma

PS C:\Users\utente> Get-Disk | Where-Object { $_.FriendlyName -eq 'XXXXX' }
PS C:\Users\utente>

There is your issue. It seems like the FriendlyName you specify after “-eq” does not fit to any of your disks.

very bad :pensive:

Thank you veru much!