Finding files with PowerShell
From my May post, in which I started using PowerShell, I have been using it to do a search I would previously have used a Linux environment to use find
for.
Searching a network share
The first challenge was to change the working directory to be that of the network share I wanted to search. cd
does not support this, but Set-Location
does:
Set-Location \\my_server\my_share
Next I found out how to search for files with PowerShell. This is supported directly by the Get-Childitem
cmdlet with the -Recurse
and -Include
/-Exclude
options:
Get-ChildItem -Path '.\Some_Pattern*' -Recurse -Include *.sub
Next, I wanted to find a list of directories that contained more than one matching file (this was my goal).
First I needed to find the name of the property in the results that told me the directory name containing the result. Get-Member
is the tool to use for this:
Get-ChildItem -Path '.\Some_Pattern*' -Recurse -Include *.sub | Get-Member -MemberType property
From this, I could see the property I needed was ‘Directory’, so now I can find all the ones with more than 1 match:
Get-ChildItem -Path '.\Some_Pattern*' -Recurse -Include *.sub | Group-Object Directory | Where-Object Count -GT 1