Home > Mobile >  Powershell variable output is different to running the command
Powershell variable output is different to running the command

Time:06-23

I'm trying to capture the output of a get-disk command as a variable, to re-use later on, but what is captured in the variable is entirely different to what displays when I run the command.

$DriveToUse = Read-Host -Prompt "Enter The disk number"

Write-host "You have chosen The following Disk " 

$SelectedDrive = get-disk -number $DriveToUse  

Write-host $SelectedDrive

This gives me the output:

You have chosen The following Disk 
MSFT_Disk (ObjectId = "{1}\\UKWRN02L8CQYRQ2\root/Microsoft/Win...)

However, if I just use the command itself, I would expect this:

get-disk -number $DriveToUse 

Number Friendly Name                                                                                                                                           Serial Number                    HealthStatus         OperationalStatus      Total Size Partition 
                                                                                                                                                                                                                                                       Style     
------ -------------                                                                                                                                           -------------                    ------------         -----------------      ---------- ----------
1      Lexar USB Flash Drive                                                                                                                                   AA00000000000000                 Healthy              Online                   29.81 GB MBR  

I also want it to just show me the disk number and name in the output - but that can come later, once I make it work at all :)

Can someone point me in the right direction please - I've thought about declaring the var as a different type, but can't make that work either (I'm a proper noob at powershell - can you tell?)

Thanks in hopefulness!

Mike.

CodePudding user response:

When using Write-Host you are converting the results to string

It's Just like you'll do:

$SelectedDrive.ToString()

Use Write-Output instead to avoid converting to string

Write-Output $SelectedDrive
  • Related