#Description : List all the files of a local or remote drive, run this PowerShell script using PowerShell-ISE.exe for better execution and output.
Steps:- Copy the content and paste it in Get-AllFilesSearchbyPattern.PS1 and Execute.
#Input Parameter List - Server Name, Drive Name, Extension and Pattern. If you want to search in specific Folder you need to type ‘Yes‘ if not type ‘No‘
Or You can create only the function and call the function with parameters. which is given below
For Example -
1. Get-FileBySearchingPattern HQDB001 D SQL TLOG_PowerShell No
It will traverse through all files of D Drive on HQDB001 Server for TLOG_PowerShell as it’s searching string.
2. Get-FileBySearchingPattern HQDB001 D SQL TLOG_PowerShell Yes
Enter the folder that you want to do a search
PowerShell\PowerShell1
Now, path for the search becomes \\HQDB001\d$\PowerShell\PowerShell1\.
##########################
#Part 1 – Copy the below Content Get-AllFilesSearchbyPattern.PS1
##########################
Function Get-FileBySearchingPattern
{
Param([String]$server,[char]$drive,[String]$extn,[String]$pattern,[String]$Folder)
IF ($Folder -eq ‘No’)
{
Get-ChildItem \\$server\$drive$ *.$extn -recurse -Force -ErrorAction SilentlyContinue | ? {$_.psiscontainer -eq $false} | ? {gc $_.pspath |select-string -pattern “$pattern”}|select name,DirectoryName
}
elseif($folder -eq ‘Yes’)
{
$FolderStruncture = Read-Host ” Enter the Folder Name”
if(test-path \\$server\$drive$\$FolderStruncture -pathtype container)
{
Get-ChildItem \\$server\$drive$\$FolderStruncture *.$extn -recurse -Force -ErrorAction SilentlyContinue | ? {$_.psiscontainer -eq $false} | ? {gc $_.pspath |select-string -pattern “$pattern”}|select name,DirectoryName
}
else
{
write-host “Invalid directory Structure”
}
}
else
{
Write-host “Enter the correct option”
}
}
$server=Read-Host ” Enter the servername”
$drive = Read-Host ” Enter the DriveName”
$extn = Read-Host ” Enter the extn”
$pattern=Read-Host ” Enter the pattern to search”
$SearchFolder = Read-Host ” Do you need to Search the pattern in Folder (No/Yes)”
Get-FileBySearchingPattern $server $drive $extn $pattern $SearchFolder
*****************************
Part 2: Create function Get-FileBySearchingPattern and call the function by calling parameters.
*********************
Function Get-FileBySearchingPattern
{
Param([String]$server,[char]$drive,[String]$extn,[String]$pattern,[String]$Folder)
IF ($Folder -eq ‘No’)
{
Get-ChildItem \\$server\$drive$ *.$extn -recurse -Force -ErrorAction SilentlyContinue | ? {$_.psiscontainer -eq $false} | ? {gc $_.pspath |select-string -pattern “$pattern”}|select name,DirectoryName
}
elseif($folder -eq ‘Yes’)
{
$FolderStruncture = Read-Host ” Enter the Folder Name”
if(test-path \\$server\$drive$\$FolderStruncture -pathtype container)
{
Get-ChildItem \\$server\$drive$\$FolderStruncture *.$extn -recurse -Force -ErrorAction SilentlyContinue | ? {$_.psiscontainer -eq $false} | ? {gc $_.pspath |select-string -pattern “$pattern”}|select name,DirectoryName
}
else
{
write-host “Invalid directory Structure”
}
}
else
{
Write-host “Enter the correct option”
}
}