Find Empty Directories

$directories="\\FILESERVER01\SHARE01"

# Dynamic Credential method 1
$who = whoami
	if ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;}
    else {$username=$who+"-admin";}
#$password = Read-Host -Prompt "Input the password for account $username" -AsSecureString
$password=convertto-securestring "PASSWORD" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
$elevate = New-PSSession -ComputerName "JUMPBOX01" -Credential $cred

function listEmptyFolders($items){
    $items | foreach {
        Get-ChildItem $_ -Recurse –ErrorAction SilentlyContinue | Where-Object -FilterScript {$_.PSIsContainer -eq $True} | Where-Object -FilterScript {($_.GetFiles().Count -eq 0) -and $_.GetDirectories().Count -eq 0} | Select-Object -ExpandProperty FullName
        }
}

listEmptyFolders $directories

Leave a Reply

Your email address will not be published. Required fields are marked *