Tuesday, April 30, 2019

Power Shell - Search text in files from one directory

If you want to search a specific string within lots of files. Below is what you can do. I use this to search string in SSIS packages:

1. open text file and save as ".ps1";

2. write the following code:

$Path = "your file directory"
$Text = "text you want to search"
$PathArray = @()

# This code snippet gets all the files in $Path that end in ".dtsx".
Get-ChildItem $Path -Filter "*.dtsx" |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:"
$PathArray | ForEach-Object {$_}

3. Run the code through powershell_ise.exe which is the GUI for you to look at the data.
(usually it is under c:\Windows\System32\WindowsPowerShell\v1.0\)

No comments:

Post a Comment