Reputation: 412
I have a bunch of lists of documents generated in powershell using this command:
Get-ChildItem -Recurse |
Select-String -Pattern "acrn164524" |
group Path |
select Name > test.txt
In this example it generates a list of files containing the string acrn164524 the output looks like this:
Name
----
C:\data\logo.eps
C:\data\invoice.docx
C:\data\special.docx
InputStream
C:\datanew\special.docx
I have been using
Get-Content "test.txt" | ForEach-Object {
Copy-Item -Path $_ -Destination "c:\destination\" -Recurse -Container -Force
}
However, this is an issue if two or more files have the same name and also throws a bunch of errors for any lines in the file that are not a path.
sorry if I was not clear enough I would like to keep files with the same name by appending something to the end of the file name.
Upvotes: 1
Views: 62
Reputation: 338208
You seem to want the files, not the output of Select-String
. So let's keep the files.
Get-ChildItem -Recurse -File | Where-Object {
$_ | Select-String acrn164524 -Quiet
} | Select-Object -ExpandProperty FullName | Out-File test.txt
Here
-File
will make Get-ChildItem
only return actual files. Think
about using a filter like *.txt
to reduce the workload more.-Quiet
will make Select-String
return $true
or $false
, which
is perfect for Where-Object
.Instead of Select-Object -ExpandProperty X
in order to retrieve an array of raw property values (as opposed to an array of PSObjects, which is what Select-Object
would normally do), it's simpler to use ForEach-Object X
instead.
Get-ChildItem -Recurse -File | Where-Object {
$_ | Select-String acrn164524 -Quiet
} | ForEach-Object FullName | Out-File test.txt
Upvotes: 1