PowerShell – Pattern Search – Local or Remote Folder

#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”
}

}

Advertisements

About Prashanth Jayaram

DB Technologist, Author, Blogger, Operations-Mgr at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek You can connect me via https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ https://www.tumblr.com/blog/prashantjayaram http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ http://www.toadworld.com/members/prashanthjayaram/ My Articles are published in following sites http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s