Wildcard in PowerShell - skip over directory

KingTekno 1 Reputation point
2022-06-24T19:02:56.263+00:00

Hello Team!

I am new to PowerShell and would appreciate it if you can assist me with the following.

I have these lines of codes that check whether or not the FinancialData directory is empty. Based on the directory status, the codes will return a message. Everything is working as designed; however, I would like to use a wildcard in the place of \Report\ in the path, because it will not always be that directory. Based on the software installed, the "Report" directory can be anything and there are several software configuration that can be installed and the folder will be different for each. So, I am stuck and need your assistance.

Is the FinancialData directory empty?

if( (Get-ChildItem "C:\ProgramData\Company\Files\Report\FinancialData" | Measure-Object).Count -eq 0)
{
echo "FinancialData directory is empty" | Out-File -FilePath C:\Company_QA_Audit_Log.txt -Append
} else { echo "FinancialData directory is not empty" | Out-File -FilePath C:\Company_QA_Audit_Log.txt -Append
}

Thanks so much for the help.

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,113 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,149 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2022-06-24T19:35:16.573+00:00

    See if something like this works for you:

    Get-ChildItem "C:\ProgramData\Company\Files\*" -Directory -Recurse |  
        Where-Object {$_.Name -eq "FinancialData"} |  
            ForEach-Object {          
                if ( (Get-ChildItem $_.FullName | Measure-Object).Count -eq 0) {  
                    "FinancialData directory '$($_.FullName)' is empty "  
                }  
                else {  
                    "FinancialData directory $($_.FullName) is not empty"  
                }  
            } | Out-File -FilePath C:\Company_QA_Audit_Log.txt -Append  
    
    0 comments No comments