PowerShell File Search Automation
PowerShell File Search Automation
com) PRINT
Article
One of the common requirements when working with files and folders is to get file details at a particular path.
For example, searching for all the text files in a specific folder. Often we do it manually or sort it in Windows
Explorer. This is one of the tasks which can be easily automated using Powershell.
Powershell has the concept of drives. When working with files and folders, we can use the commandlets for
drives. In our case, these cmdlets will let us retrieve and manipulate the files and folders. For example, we can list
all the files in a specific folder using the following command:
JavaScript
Copy
The above command will list the contained items, such as folders or files. You can further filter with the Filer
parameter:
This will filter based on the file name. Since we need to display the file details contained within a particular folder,
we will define a function for this.
function Get-FolderDetails
{
# Your code logic here
}
JavaScript
Copy
We will pass a parameter called $path and $extension to this function. This will be used to filter the folder based
on the files with matching extensions at the provided path.
JavaScript
Copy
Also, we want to ensure that the item we are searching in is a folder. To achieve this, we can use the where
command.
where {!$_.PSIsContainer}
JavaScript
Copy
JavaScript
Copy
Now we can easily iterate and display the various properties of the filtered files sing the following.
JavaScript
Copy
JavaScript
Copy
JavaScript
Copy
This will display file details for the text files at the specified path.
Thank you for using C# Corner