Find hidden files with PowerShell

I recently had a need to get a list of hidden files in a directory tree. PowerShell made it fairly easy to knock out the list in a quick and dirty fashion, by checking if the file Attributes contain the string “Hidden”:

Get-ChildItem C:\myfolder\ -Recurse -Force | Where-Object { $_.Attributes -like "*Hidden*" } | Select FullName

However, this is a little less than scientific. What if there were an attribute called “NotHidden”? This search would pickup files with that attribute, as well as files with the “Hidden” attribute.
NOTE:  The “NotHidden” attribute is completely hypothetical, but proves the point that wildcard searches are not always the best.

In order to search more specifically for just the “Hidden” attribute, you could use the following:

PS C:\> Get-ChildItem C:\myfolder\ -Recurse -Force | Where { ($_.Attributes.ToString() -Split ", ") -Contains "Hidden" } | Select FullName

This code looks at all items in the C:\myfolder\ folder and all sub-folders. It takes the Attributes field for each item and splits the contents on “, ” – this creates an array of attributes, which we can then check for the specific “Hidden” parameter.

The same method can be used for other attributes, like “Archive” and “ReadOnly.”

Leave a Reply