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

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

function Get-VMFolderPath

    Get vm folder path. From Datacenter to folder that keeps the vm.


    This function returns vm folder path. As a parameter it takes the 
	current folder in which the vm resides. This function can throw
	either 'name' or 'moref' output. Moref output can be obtained
	using the -moref switch.


    get-vm 'vm123' | get-vmfolderpath

    Function will take folderid parameter from pipeline

    get-vmfolderpath (get-vm myvm123|get-view).parent

    Function has to take as first parameter the moref of vm parent
	Parameter will be the folderX moref

    get-vmfolderpath (get-vm myvm123|get-view).parent -moref

    Instead of names in output, morefs will be given.

	.Parameter folderid

    This is the moref of the parent directory for vm.Our starting
	point.Can be obtained in serveral ways. One way is to get it
	by: (get-vm 'vm123'|get-view).parent  
	or: (get-view -viewtype virtualmachine -Filter @{'name'=
   .Parameter moref

    Add -moref when invoking function to obtain moref values


    NAME:  Get-VMFolderPath

    AUTHOR: Grzegorz Kulikowski

    LASTEDIT: 09/14/2012
	NOT WORKING ? #powercli @ 



[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]

	$folderparent=get-view $folderid
	if ($ -ne 'vm'){
		if ($folderparent.parent){
			if($moref){get-vmfolderpath $folderparent.parent.tostring() -moref}
	}else {
	if ($moref){
	return (get-view $folderparent.parent).moref.tostring()+"\"+$folderparent.moref.tostring()+"\"+$path
	}else {
			return (get-view $folderparent.parent).name.toString()+'\'+$'\'+$path

12 thoughts on “Get-VMFolderPath to check in which vm folder our vm resides

  1. 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.

    • 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 ?

  2. 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

    • 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.

  3. Pingback: Speeding up scripts with Get-View | sweetTech
  4. 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!

    • Can you check :
      $a = get-vm ‘My VM’
      $a | get-vmfolderpath
      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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s