external help file | Module Name | online version | schema |
---|---|---|---|
PSScriptTools-help.xml |
PSScriptTools |
2.0.0 |
Test if a folder is empty of files.
Test-EmptyFolder [-Path] <String[]> [-Passthru] [<CommonParameters>]
This command will test if a given folder path is empty of all files anywhere in the path. This includes hidden files. The command will return True even if there are empty sub-folders. The default output is True or False but you can use -Passthru to get more information. See examples.
PS C:\> Test-EmptyFolder c:\work
False
Test a single folder from a parameter.
PS C:\> Get-ChildItem c:\work -Directory | Test-EmptyFolder -passthru
Path Name IsEmpty Computername
---- ---- ------- ------------
C:\work\A A False DESK10
C:\work\alpha alpha False DESK10
C:\work\B B True DESK10
C:\work\data data False DESK10
C:\work\demo3 demo3 True DESK10
C:\work\demos demos False DESK10
...
Test child folders under C:\work.
PS C:\> Get-ChildItem c:\work -Directory | Test-EmptyFolder -passthru |
Where-object {$_.Isempty} |
Foreach-Object { Remove-Item -LiteralPath $_.path -Recurse -force -whatif}
What if: Performing the operation "Remove Directory" on target "C:\work\demo3".
What if: Performing the operation "Remove Directory" on target "C:\work\installers".
What if: Performing the operation "Remove Directory" on target "C:\work\new".
What if: Performing the operation "Remove Directory" on target "C:\work\sqlback".
What if: Performing the operation "Remove Directory" on target "C:\work\todd".
What if: Performing the operation "Remove Directory" on target "C:\work\[data]".
Find all empty sub-folders under C:\Work and pipe them to Remove-Item. This is one way to remove empty folders. The example is piping objects to ForEach-Object so that Remove-Item can use the -LiteralPath parameter, because C:\work[data] is a non-standard path.
Write a test object to the pipeline.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Enter a file system path like C:\Scripts.
Type: String[]
Parameter Sets: (All)
Aliases: PSPath
Required: True
Position: 0
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/