Get-VMFolderPath to check in which vm folder our vm resides

by Grzegorz Kulikowski

Recently i wrote a function to check in which vm folder particular vm resides. I thought i could share it 😉 This function can output folder names or folder morefs when -moref switch is specified. Sample output would be this:
get-vm ‘vm123’ | get-vmfolderpath
MY DC1\vm\test123\ntest\btest\xtest\

Get-VMfolderPath (get-vm vm1).folderid
MY DC1\vm\test123\ntest\btest\xtest\

When you want to instead of names , folder morefs:

Get-VMfolderPath (get-vm vm1).folderid -moref or
get-vm ‘vm123’ |Get-VMfolderPath -moref
Datacenter-datacenter-242\Folder-group-v243\Folder-group-v740\Folder-group-v1434\Folder-group-v1435\Folder-group-v1436\



You may also like

13 comments

psvmware September 14, 2012 - 12:26 pm

Thanks to Hendrick who spot my typos ! 🙂

Reply
JD November 1, 2012 - 12:23 am

GREAT Funtion.. one thing to note is it does not work when powercli is connected to multiple vcenters

set-powercliconfiguration -defaultVIservermode multiple

I believe this is because it calls the parent up to a null value.

Reply
psvmware November 7, 2012 - 12:28 pm

Hi JD, i have tested it when connected to multiple VC. It works ok. Can you tell more what/when it is not working ? can you tell what version are you using ? Get-PowerCLIVersion ?

Reply
Dion February 7, 2018 - 12:48 am

I can confirm that this does not work when connected to 2 vcenter servers at the same time.

If connected to 2 servers I get:

System.Object[]\System.Object[]\SupportCase\SupportVeeamJob9\

Reply
Dion February 7, 2018 - 12:49 am

Meet in #powercli to discuss further

Reply
Muhammad May 28, 2014 - 6:49 pm

PowerCLI C:\> Connect-VIServer -Server abc.dcsprod.dcsroot.local -User dcsutil\auamu -Password “Tr1234”

Name Port User
—- —- —-
abc.dcsprod.dcsr… 443 dcsutil\auamu

PowerCLI C:\> get-vm -name ‘vmname’ | .\get-vmfolderpath.ps1
PowerCLI C:\>

The script is doing nothing for me why?

Please advice what I doing wrong as I pasted your script as Powershell script file and run it

Reply
psvmware May 28, 2014 - 7:00 pm

Muhammad, first load the function so:
cd c:\folder\where\is\the\ps1\file
. ./get-vmfolderpath.ps1
and then
get-vm ‘vm123′ |Get-VMfolderPath

or just copy paste the function to your powercli/powershell window.

Reply
Speeding up scripts with Get-View | sweetTech March 2, 2015 - 4:54 pm

[…] swapped out LucD’s function for psvmware’s function Get-VMFolderPath and changed my code to the […]

Reply
Kyle Espinoza February 22, 2016 - 9:53 pm

Hi, I get a LOT of input for command get-vm “roast beef sandwich” | get-vmfolderpath – it looks like the path is repeating. Is this a known issue? For example, I’m getting something like TestDatacenter\VM\Test\Sandwiches\RedMeat\roast beef sandwich\TestDatacenter\VM\Test\Sandwiches\RedMeat\roast beef sandwich, or it will start outputting paths of different VM’s or folders. This is an amazing script if I can get it to work for me. Thanks!

Reply
psvmware February 23, 2016 - 12:42 pm

Can you check :
$a = get-vm ‘My VM’
$a | get-vmfolderpath
or
get-vmfolderpath -folderid $a.folderid

and that
$a.count is not more than 1 ?

Ideally, join #powercli on freenode, and we can check it there. I tested it today, and that works for me, no problems.

Reply
Anonymous February 20, 2017 - 8:24 pm

Thanks, saved some time!

Reply
Dmytro Zhyhalkin May 4, 2017 - 4:53 pm

$VM_name = Read-Host Get vm name
$VM = Get-VM $VM_name
$VM.ExtensionData.Config.Files

Reply
Anonymous February 9, 2024 - 2:11 pm

Great, thank you 🙂

Reply

Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish