Searching for files
In this recipe, we will search for files based on filenames, attributes, and content.
How to do it...
Let's explore different ways to use Get-ChildItem
to search for files:
- Open PowerShell ISE as an administrator.
- Add the following script and run it:
#search for file with specific extension $path = "C:\Temp" Get-ChildItem -Path $path -Include *.sql -Recurse #search for file based on date creation #use LastWriteTime for date modification [datetime]$startDate = "2015-05-01" [datetime]$endDate = "2015-05-24" #note date is at 12 midnight #sample date Sunday, May 24, 2015 12:00:00 AM #search for the file Get-ChildItem -Path $path -Recurse | Where-Object CreationTime -ge $startDate | Where-Object CreationTime -le $endDate | Sort-Object -Property LastWriteTime #list files greater than 10MB Get-ChildItem $path -Recurse | Where-Object Length -ge 10Mb | Select-Object Name, @{Name="MB";Expression={"{0:N2}" -f ($_...