Posted On March 29, 2019

Find Empty Directories

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> 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 *

Related Post

PowerShell: Force Outlook to Compact On Exiting

Forcing Outlook to return free disk space to RDS nodes upon user exiting would be…

PowerShell: Quickly Add Users into Groups on a List of Computers

$newVmNames=@( 'SERVER0001', 'SERVER0002' ) $newUsers=@( 'domain\user1' ) $groupNames='Administrators','Remote Desktop Users' $newVmNames|%{ invoke-command -computername $_ {…

PowerShell: Function to Get Group Members as a Bypass Orphanated SID Errors

Problem: [TESTSERVER]: PS C:\Users\administrator> Get-LocalGroupMember 'Remote Desktop Users' Get-LocalGroupMember : Failed to compare two elements…