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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
# http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/26/use-powershell-to-work-with-windows-explorer.aspx
$o = New-Object -com Shell.Application
$folder = $o.NameSpace(0x11)
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx
# ShellSpecialFolderConstants.ssfDRIVES == 0x11
$items = $folder.Items()
for ($i= 0; $i -lt $items.Count; $i++) {
write-output ([string]$i + ": " + $items.Item($i).Name)
}
$choice = Read-Host "Make your choice"
$android = $items.Item([int]$choice)
$root = $android.GetFolder()
# FolderItem versus FolderItems
$maxdepth = Read-Host "Max depth"
Function Write-Items($item, $depth, $maxdepth) {
if ($depth -ge $maxdepth) {
return;
}
$indent = ""
for ($i = 0; $i -lt $depth; $i++) {
$indent += " ";
}
#write-output ($indent + "Name: " + $item.name)
if ($item.Title) {
$hash = @{
Name = $item.Title
Size = $null #$item.ParentFolder.GetDetailsOf($item, 2)
Modified = $null #$item.ParentFolder.GetDetailsOf($item, 3)
Parent = $item.ParentFolder.Title
Level = $depth
}
$Object = New-Object PSObject -Property $hash
write-output $object
} else {
$hash = @{
Name = $item.Name
Size = $item.Parent.GetDetailsOf($item, 2)
Modified = $item.Parent.GetDetailsOf($item, 3)
Parent = $item.Parent.Title
Level = $depth
}
$Object = New-Object PSObject -Property $hash
Write-Output $object
}
if ($item.Count -gt 0) {
# $item is a folder with its own items
for ($i = 0; $i -lt $item.Count) {
$item2 = $item.item($i)
Write-Items $item2 $depth+1 $maxdepth
}
}
else {
if ($item.Items) {
$items = $item.Items()
if ($items.Count -gt 0) {
foreach ($i in $items) {
if ($i.IsFolder) {
$folder = $i.GetFolder()
Write-Items $folder ($depth+1) $maxdepth
}
else {
Write-Items $i ($depth+1) $maxdepth
}
}
}
}
else {
# .Count == 0 and no Items present
# we don't need to do anything further here
}
}
}
Write-Items $root 0 $maxdepth |
Partager