Search code examples
bashpowershellawksedpowercli

Filter the output of powercli command to only had a part of it


The output of the command is:

PS /root> Get-VM -Name alex-deploy2-000638 | Get-Harddisk

CapacityGB      Persistence                                                    Filename
----------      -----------                                                    --------
60.000          Persistent           …1] alex-deploy2-000638/alex-deploy2-000638_2.vmdk

I need to always have the last part after the /

alex-deploy2-000638_2.vmdk

Is there a way to filter that output to cut everything else that is not disk-name.vmdk?


Solution

  • First, select the property you are interested in (eg: FileName). Then, you can use Split-Path -Leaf to get the filename without the folder.

    Since your Get-HardDisk command can return multiple disks, I included a Foreach for the last part, to make sure it work with a single disk and multiple disks.

    (Get-VM -Name alex-deploy2-000638 | Get-Harddisk).FileName  | % {$_ | Split-Path -Leaf}