Testing if a folder is empty is pretty simple and has been discussed many times. Mostly using test-path or get-childitem Example: Powershell test if folder empty
I'm working on a script for auditing and correcting file NTFS permissions on SMB shares in my enterprise environment.
I've run into something that would at first appear simple but I'm not able to find a simple solution. I'm wondering if there is a cmdlet that would help. That is, differentiating between if a given folder is empty or I don't have access to it.
The problem is all the solutions that I see assume that you have access to the folder.
I was thinking that I was going to have to write a greasy function to do multiple tests then catching and testing error codes or something. But I thought I would ask the smart coders here at Stack Overflow before going down that rabbithole.
Is there a more elegant way to test this? A Test-MyAccess cmdlet or something?
Test-Path returns the same results for empty folders and folders with no access. Get-childitem returns null/empty for empty folders and folders with no access. Except a no access folder has an error.
PS H:\PowerShell\NTFS> Test-Path $EmptyFolder
True
PS H:\PowerShell\NTFS> Test-Path $EmptyFolder\*
False
PS H:\PowerShell\NTFS> Test-Path $NoAccessFolder
True
PS H:\PowerShell\NTFS> Test-Path $NoAccessFolder\*
False
PS H:\PowerShell\NTFS> Get-ChildItem -Path $EmptyFolder
PS H:\PowerShell\NTFS> Get-ChildItem -Path $NoAccessFolder
Get-ChildItem : The specified network name is no longer available.
At line:1 char:1
+ Get-ChildItem -Path $NoAccessFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\\server.domain....\Sufolder1\Sub3:String) [Get-ChildItem], IOException
......