forked from RamblingCookieMonster/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-FolderEntry.ps1
138 lines (112 loc) · 5.43 KB
/
Get-FolderEntry.ps1
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function Get-FolderEntry {
<#
.SYNOPSIS
Lists all folders under a specified folder regardless of character limitation on path depth.
.DESCRIPTION
Lists all folders under a specified folder regardless of character limitation on path depth.
This is based on Boe's Get-FolderItem command here: http://gallery.technet.microsoft.com/scriptcenter/Get-Deeply-Nested-Files-a2148fd7
.FUNCTIONALITY
Computers
.PARAMETER Path
One or more paths to search for subdirectories under
.PARAMETER ExcludeFolder
One or more paths to exclude from query
.EXAMPLE
Get-FolderEntry -Path "C:\users"
FullPathLength FullName FileCount
-------------- -------- ---------
9 C:\Users\ 1
23 C:\Users\SomeUser\ 7
31 C:\Users\SomeUser\AppData\ 0
37 C:\Users\SomeUser\AppData\Local\ 0
47 C:\Users\SomeUser\AppData\Local\Microsoft\ 0
...
Description
-----------
Returns all folders under the users folder.
.EXAMPLE
Get-FolderEntry -Path "C:\users" -excludefolder "C:\Users\SomeUser\AppData\Local\Microsoft\"
FullPathLength FullName FileCount
-------------- -------- ---------
9 C:\Users\ 1
23 C:\Users\SomeUser\ 7
31 C:\Users\SomeUser\AppData\ 0
37 C:\Users\SomeUser\AppData\Local\ 0
52 C:\Users\SomeUser\AppData\Local\Microsoft Help\ 0 #NOTE that we skipped the excludefolder path
...
Description
-----------
Returns all folders under the users folder, excluding C:\Users\SomeUser\AppData\Local\Microsoft\ and all subdirectories
.INPUTS
System.String
.OUTPUTS
System.IO.RobocopyDirectoryInfo
.NOTES
Name: Get-FolderItem
Author: Boe Prox
Date Created: 31 March 2013
Updated by rcm
#>
[cmdletbinding(DefaultParameterSetName='Filter')]
Param (
[parameter(
Position=0,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[Alias('FullName')]
[string[]]$Path = $PWD,
[parameter(ParameterSetName='Filter')]
[string[]]$Filter = '*.*',
[parameter(ParameterSetName='Exclude')]
[string[]]$ExcludeFolder
)
Begin {
#Define arguments for robocopy and regex to parse results
$array = @("/L","/S","/NJH","/BYTES","/FP","/NC","/NFL","/TS","/XJ","/R:0","/W:0")
$regex = "^(?<Count>\d+)\s+(?<FullName>.*)"
#Create an arraylist
$params = New-Object System.Collections.Arraylist
$params.AddRange($array)
}
Process {
ForEach ($item in $Path) {
Try {
$item = (Resolve-Path -LiteralPath $item -ErrorAction Stop).ProviderPath
If (-Not (Test-Path -LiteralPath $item -Type Container -ErrorAction Stop)) {
Write-Warning ("{0} is not a directory and will be skipped" -f $item)
Return
}
If ($PSBoundParameters['ExcludeFolder']) {
$filterString = ($ExcludeFolder | %{"'$_'"}) -join ','
$Script = "robocopy `"$item`" NULL $Filter $params /XD $filterString"
}
Else {
$Script = "robocopy `"$item`" NULL $Filter $params"
}
Write-Verbose ("Scanning {0}" -f $item)
#Run robocopy and parse results into an object.
Invoke-Expression $Script | ForEach {
Try {
If ($_.Trim() -match $regex) {
$object = New-Object PSObject -Property @{
FullName = $matches.FullName
FileCount = [int64]$matches.Count
FullPathLength = [int] $matches.FullName.Length
} | select FullName, FileCount, FullPathLength
$object.pstypenames.insert(0,'System.IO.RobocopyDirectoryInfo')
Write-Output $object
} Else {
Write-Verbose ("Not matched: {0}" -f $_)
}
} Catch {
Write-Warning ("{0}" -f $_.Exception.Message)
Return
}
}
} Catch {
Write-Warning ("{0}" -f $_.Exception.Message)
Return
}
}
}
}