-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-Get-FolderPath.ps1
More file actions
47 lines (40 loc) · 1.16 KB
/
Copy pathfunction-Get-FolderPath.ps1
File metadata and controls
47 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Get the full folder path of a list of VMs
.DESCRIPTION
Get the full folder path of a list of VMs
.INPUTS
n/a
.OUTPUTS
Outputs to screen
.NOTES
Website: www.amikkelsen.com
Author: Anders Mikkelsen
Creation Date: 2026-02-10
.EXAMPLE
Get-VM | Get-VMFolderPath | Format-Table -AutoSize
Get-VMFolderPath -VM ( Get-VM -Name "<vmname>" )
#>
Function Get-VMFolderPath {
Param (
[Parameter( Mandatory = $true, ValueFromPipeline = $true )]
[VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl[]] $VM
)
Process {
Foreach ( $objVM in $VM ) {
$folder = $objVM.Folder
$pathParts = @()
# Walk up the folder hierarchy
While ( $folder -and $folder.Name -ne "vm") {
$pathParts += $folder.Name
$folder = $folder.Parent
}
# Reverse to get root-to-leaf order
$fullPath = ( $pathParts | Sort-Object -Descending ) -join "/"
[PSCustomObject]@{
VMName = $objVM.Name
FolderPath = $fullPath
}
}
}
}