Gathering Strings with PowerShell
Using PowerShell to Extract Strings from Files: A Quick and Efficient Solution
I needed a quick way to gather information about a DLL installed on my computer without downloading additional software (Strings - Sysinternals) or transferring the DLL to another machine hosting my VMs. I wondered if I could utilize PowerShell for this task.
I turned to ChatGPT, and asked, "Does Windows 11 PowerShell have a string command?" In no time, I had the answer and the following script, which felt a bit like cheating but met my need for a quick solution.
function Get-Strings {
param (
[string]$Path,
[int]$MinLength = 4
)
if (-Not (Test-Path $Path)) {
Write-Error "File not found: $Path"
return
}
$bytes = [System.IO.File]::ReadAllBytes($Path)
$str = [System.Text.Encoding]::ASCII.GetString($bytes)
$str -split "`0|`n|`r" | Where-Object { $_.Length -ge $MinLength }
}
To use the function:
Get-Strings -Path "C:\Path\To\Your\File.dll" -MinLength 4
Running the `Get-Strings` command on the DLL produced the typical gobbledygook but also displayed a list of strings like the screenshot below.
There was nothing malicious about this DLL, and the displayed strings matched the program that uses this DLL, making this approach a success.