-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathTest-EmptyFolder.ps1
78 lines (70 loc) · 2.64 KB
/
Test-EmptyFolder.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
Function Test-EmptyFolder {
[CmdletBinding()]
[OutputType("Boolean", "EmptyFolder")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter a file system path like C:\Scripts.", ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[alias("PSPath")]
[string[]]$Path,
[Parameter(HelpMessage = "Write a test object to the pipeline")]
[switch]$Passthru
)
Begin {
Write-Verbose "Starting $($MyInvocation.MyCommand)"
} #Begin
Process {
foreach ($item in $path) {
$cPath = (Convert-Path -literalpath $item)
Write-Verbose "Measuring $cPath on $([System.Environment]::MachineName)"
if (Test-Path -literalpath $cPath) {
$d = [System.IO.DirectoryInfo]::new($cPath)
If ($psversiontable.psversion.major -gt 5 ) {
#this .NET class is not available in Windows PowerShell 5.1
$opt = [System.IO.EnumerationOptions]::new()
$opt.RecurseSubdirectories = $True
$opt.AttributesToSkip = "SparseFile", "ReparsePoint"
Try {
$files = $d.GetFiles("*", $opt)
}
Catch {
Write-Warning $_.exception.message
}
} #if newer that Windows PowerShell 5.1
else {
Write-Verbose "Using legacy code"
Try {
$files = $d.GetFiles("*", "AllDirectories")
}
Catch {
Write-Warning $_.exception.message
}
}
If ($files.count -eq 0) {
$Empty = $True
}
else {
Write-Verbose "Found $($files.count) files"
$Empty = $False
}
if ($Passthru) {
[PSCustomObject]@{
PSTypeName = 'EmptyFolder'
Path = $cPath
Name = (Split-Path -Path $Cpath -leaf)
IsEmpty = $Empty
Computername = [System.Environment]::MachineName
}
}
else {
$Empty
}
}
else {
Write-Warning "Can't find $Path on $([System.Environment]::MachineName)"
}
} #foreach item
}
End {
Write-Verbose "Ending $($MyInvocation.MyCommand)"
}
}