Scanning Directories for Files with PowerShell
Learn how Scanning Directories for Files with PowerShell can help you locate specific files, making it easy to identify their paths, sizes, and modification dates.
Script Overview
The PowerShell script provided below searches for .xlsx
files starting from the root directory C:\
. It recursively scans through all subdirectories and returns a list of files that match the specified criteria. For each found file, it displays the full file path, file size, and the last modified date.
Get-ChildItem -Path C:\ -Recurse -Include *filenamehere.xlsx | Select-Object FullName, Length, LastWriteTime
Explanation of the Script
- Get-ChildItem: This cmdlet retrieves the items (files and directories) from a specified location. It can be used to list files and directories in a given path.
- **-Path C:**: This specifies the starting point for the search, which is the root directory
C:\
. You can change this to any directory you want to start your search from. - -Recurse: This parameter tells PowerShell to include all subdirectories in the search. Without this parameter, only the files in the specified directory would be listed.
- *-Include filenamehere.xlsx: This parameter filters the results to include only the files that match the specified pattern. Replace
*filenamehere.xlsx
with your desired file pattern. The asterisk*
is a wildcard that matches any sequence of characters. - Select-Object FullName, Length, LastWriteTime: This cmdlet is used to select specific properties of the objects returned by
Get-ChildItem
. Here, it selects the full path (FullName
), file size in bytes (Length
), and the last modified date and time (LastWriteTime
).
How to Use the Script
- Open PowerShell: Launch PowerShell on your Windows machine. You can do this by searching for “PowerShell” in the Start menu.
- Run the Script: Copy the script and paste it into the PowerShell window. Press
Enter
to execute. - Review the Output: The script will list all
.xlsx
files starting from the root directory, displaying their full paths, sizes, and last modified dates.
Customizing the Script
- Change the File Type: If you’re looking for a different type of file, simply replace
*.xlsx
with the desired file extension (e.g.,*.docx
for Word documents). - Specify a Different Path: To search in a different directory, change
-Path C:\
to the path of the desired directory (e.g.,-Path D:\Documents
).
Conclusion
By utilizing this PowerShell script, you can find specific files across directories. It’s a great script to quickly find files you are looking for.
Discover more from Patrick Domingues
Subscribe to get the latest posts sent to your email.
Hi Patrick,
I find your blog amazing.
Do you have a code that can extract all subsites with their size (+ their libraries) into an excel file?
Thanks,
Martin