03. April 2021
Power Shell | Skripte
Filesystem
Loop through results from Select-String
Here is an example that greps for a string and uses the results in a loop to determine if some action should be taken
<pre class="EnlighterJSRAW" data-enlighter-group="" data-enlighter-highlight="" data-enlighter-language="powershell" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-theme="" data-enlighter-title="">$pattern = "tachytelic"
$files = Select-String -Path "d:\script\*.txt" -Pattern $pattern
foreach ($file in $files) {
$filename=$file.Filename
$item = get-item $file.Path
"File '{0}' matches the pattern '{1}'" -f $item.FullName, $pattern
"It was last modified on {0}" -f $item.LastWriteTime
$response = Read-Host -Prompt "Set the archive bit on this file?"
If ($response -eq 'Y') {
$item.attributes="Archive"
}
}