Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Open PowerShell ISE as an administrator.

  2. 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 ($_.Length/1MB)}} | 
    Sort-Object -Property Length -Descending...