2024-01-25 13:37:12 +01:00
|
|
|
Script: *search-files.ps1*
|
|
|
|
========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
This PowerShell script searches for the given text pattern in the given files.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./search-files.ps1 [[-textPattern] <String>] [[-filePattern] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
-textPattern <String>
|
|
|
|
Specifies the text pattern to search for
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
-filePattern <String>
|
|
|
|
Specifies the files to search
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./search-files.ps1 UFO *.ps1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FILE LINE
|
|
|
|
---- ----
|
|
|
|
/home/Markus/PowerShell/scripts/check-month.ps1 17: $MonthName = (Get-Date -UFormat %B)
|
|
|
|
...
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-08-15 09:51:46 +02:00
|
|
|
Searches for text in files
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
2024-08-15 09:51:46 +02:00
|
|
|
This PowerShell script searches for the given text pattern in the given files.
|
2024-05-19 10:25:56 +02:00
|
|
|
.PARAMETER textPattern
|
|
|
|
Specifies the text pattern to search for
|
|
|
|
.PARAMETER filePattern
|
|
|
|
Specifies the files to search
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./search-files.ps1 UFO *.ps1
|
|
|
|
|
|
|
|
FILE LINE
|
|
|
|
---- ----
|
|
|
|
/home/Markus/PowerShell/scripts/check-month.ps1 17: $MonthName = (Get-Date -UFormat %B)
|
|
|
|
...
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
param([string]$textPattern = "", [string]$filePattern = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
function ListLocations { param([string]$textPattern, [string]$filePattern)
|
|
|
|
$list = Select-String -path $filePattern -pattern "$textPattern"
|
|
|
|
foreach($item in $list) { New-Object PSObject -Property @{ 'FILE'="$($item.Path)"; 'LINE'="$($item.LineNumber):$($item.Line)" } }
|
|
|
|
"✔️ Found $($list.Count) lines containing '$textPattern' in $filePattern."
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-08-15 09:51:46 +02:00
|
|
|
if ($textPattern -eq "" ) { $textPattern = Read-Host "Enter the text pattern, e.g. 'UFO'" }
|
|
|
|
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern, e.g. '*.ps1'" }
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
ListLocations $textPattern $filePattern | Format-Table -property FILE,LINE -autoSize
|
2022-11-17 20:02:26 +01:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of search-files.ps1 as of 08/15/2024 09:50:53)*
|