Help Request - Tell me where my Syntax is wrong! (Powershell)
Agent6376
Member Posts: 201
Here’s the gist of what I’m trying to do – I don’t know if this is the best way of doing it or not, but I’m fairly confident that this is something that can be done in Powershell.
I have a list of filenames that I believe are located on the file server, and these files are currently setting in a text file. Unfortunately, most of the files that I still need to locate are not an exact match. There’s a backstory to it why, but I suspect that the filenames in the list are at least contained in the actual filename. I know that sounds confusing, but for example:
In the list: F-6.3-005_1
On the server: F-6.3-005_1Crane Inspection Report Form.pdf
So I do have a match, but just not on the entire file name. I know that Powershell allows the –like parameter when using the Where-Object cmdlet, and it allows wildcards, so I adjusted the filename list to have an asterisk before and after the filename. So in reality, the list actually reads the file as *F-6.3-005_1*.
I want to perform a search of all files on the server, using the filenames as what I’m searching for, and I want to results of the search to come back in a log (CSV or whatever).
What I’ve done is as follows:
$files = Get-Content C:\filenames.txt
Get-ChildItem (Directory) | Where-Object {$_.Name –like $files} | Export-CSV C:\output.csv
I think my syntax is wrong where I call the variable $files in the Where-Object, and I’m not sure how to correct it. I played around with the idea of forcing it through a ForEach statement, but again, I think my syntax is wrong. (and that may not even be necessary, as I’ve read that Powershell will already run through the content as an array)
I have a list of filenames that I believe are located on the file server, and these files are currently setting in a text file. Unfortunately, most of the files that I still need to locate are not an exact match. There’s a backstory to it why, but I suspect that the filenames in the list are at least contained in the actual filename. I know that sounds confusing, but for example:
In the list: F-6.3-005_1
On the server: F-6.3-005_1Crane Inspection Report Form.pdf
So I do have a match, but just not on the entire file name. I know that Powershell allows the –like parameter when using the Where-Object cmdlet, and it allows wildcards, so I adjusted the filename list to have an asterisk before and after the filename. So in reality, the list actually reads the file as *F-6.3-005_1*.
I want to perform a search of all files on the server, using the filenames as what I’m searching for, and I want to results of the search to come back in a log (CSV or whatever).
What I’ve done is as follows:
$files = Get-Content C:\filenames.txt
Get-ChildItem (Directory) | Where-Object {$_.Name –like $files} | Export-CSV C:\output.csv
I think my syntax is wrong where I call the variable $files in the Where-Object, and I’m not sure how to correct it. I played around with the idea of forcing it through a ForEach statement, but again, I think my syntax is wrong. (and that may not even be necessary, as I’ve read that Powershell will already run through the content as an array)
Comments
-
Agent6376 Member Posts: 201A friend of a friend found a script in the MS Powershell repository for this:
Script PowerShell: Find Strings In Specific Files And Output To A Log File
Snagged the output of the list, put it into a text file
$files = Get-Content C:\Files.txt
copy $files \PlaceforFiles