-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathInvoke-WindowsSearch.ps1
52 lines (47 loc) · 2.11 KB
/
Invoke-WindowsSearch.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function Invoke-WindowsSearch {
param (
[Parameter()][string] $SearchString = "password",
[Parameter()][string] $OutputPath = $null
)
$SearchString = $SearchString.Replace("'", "''")
$query = "SELECT System.ItemName, System.ItemPathDisplay, System.DateModified, System.Size, System.Search.AutoSummary, System.Author, System.Title, System.Keywords, System.Subject, System.FileExtension FROM SystemIndex WHERE CONTAINS('$SearchString')"
$provider = "Provider=Search.CollatorDSO.1;Extended Properties='Application=Windows'"
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter -ArgumentList $query, $provider
$results = New-Object System.Data.DataSet
try {
$adapter.Fill($results)
$table = $results.Tables[0]
if ($table.Rows.Count -gt 0) {
# Convert System.String[] to single strings
foreach ($row in $table.Rows) {
if ($row["System.Author"] -is [System.Array]) {
$row["System.Author"] = [string]::Join("; ", $row["System.Author"])
}
if ($row["System.Keywords"] -is [System.Array]) {
$row["System.Keywords"] = [string]::Join("; ", $row["System.Keywords"])
}
}
if ($OutputPath) {
# Export to CSV
$table | Export-Csv -Path $OutputPath -NoTypeInformation
Write-Output "Results exported to $OutputPath"
} else {
# Display to standard out
$table #| Format-Table -AutoSize
}
} else {
Write-Output "No results found for the search string '$SearchString'."
}
} catch {
if ($_ -match "0x80004005") {
Write-Output "No results found for the search string '$SearchString'."
} else {
Write-Output "An error occurred: $_"
}
}
}
# Example usage:
# To display the results to standard out
Invoke-WindowsSearch -SearchString "something"
# To save the results to a CSV file
Invoke-WindowsSearch -SearchString "something" -OutputPath "WindowsSearchResults.csv"