Link to home
Create AccountLog in
Avatar of Contigo1
Contigo1

asked on

PowerShell script to Get-ChildItem based on file contents

Hi,

I am trying to write a PowerShell script to get the fullname and last write time of files on a hard drive. The fullname for the files I need to get the information for is stored in a .txt file. I then need the script to put the export into a CSV\Excel file.

So far I have come up with:

foreach ($filename in get-content "C:\Test.txt") {Get-ChildItem | Select-Object Fullname, LastWriteTime | Export-CSV c:\Testingold01.csv}

Open in new window


However it is not finding the files I have specified in the .txt file.

Sorry if my explaination is not very good.

Any help would be appreciated.

Thanks
Avatar of SubSun
SubSun
Flag of India image

Try..
$Report = @()
foreach ($filename in get-content "C:\Test.txt") {
$Report += Get-ChildItem -Recurse | ? {$_.Fullname -Match $filename} | Select-Object Fullname, LastWriteTime}
$Report | Select * | Export-CSV c:\Testingold01.csv -NoTypeInformation

Open in new window

Avatar of Contigo1
Contigo1

ASKER

Hi,

I just ran this script and it still doesnt seem to be picking up the file fullnames out of the txt file specified.

It is instead running a Get-ChildItem -recurse on the whole drive when I run the script.

Thanks
What is there in "C:\Test.txt"?  Can you share the input file?
Hi,

I have attached the file.

Thanks
Test.txt
I think the quotation marks causing the issue as we are using get-content.. Try this..
$Report = @()
foreach ($filename in get-content "C:\Test.txt") {
$Report += Get-ChildItem -Recurse | ? {$_.Fullname -eq $($filename -replace '"')} | Select-Object Fullname, LastWriteTime}
$Report | Select * | Export-CSV c:\Testingold01.csv -NoTypeInformation

Open in new window

Also if you are not running the script from E:\ drive then specify the E:\ in script..

Replace Get-ChildItem -Recurse with Get-ChildItem E:\ -Recurse
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
@Subsun

The script is still not picking up the filenames from the txt file.

I have tried running it on both the C and E drives and having the text file on the different drives.

Could it be something to do with the $filename variable?

maybe it should be $i or something?

Matt
I just tested it and it's working for me.. Can you check if you are running the same script which I posted?
@Subsun

I have just copied the script you posted and re-run it again and it is still just doing a recurse of all the folders on the drive not the files in the text file.

in the get-childitem you are doing E:\ should this be $filename or something?

Matt
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Hi Subsun,

I have got the script to work now it was a bit of user error on my behalf.

The csv file the script spits out seems to be missing around 200 files which are in the text file and exist but they just are not showing up in the CSV file do you know what might be causing this?

Thanks
Hi Subsun,

As the drive where this script is being run has 1000's of files I have changed the Get-ChildItem E:\ -Recurse to be Get-ChildItem "E:\Testing.old" -Recurse

This means it will only include the folder location where the files are and should improve performance out my end.

Matt
If the files are not in output file that means the matching files re not found by script. The script looks for exact match, so any added space or character will cause the match fail.. You can check the full name of the file from text and match the actual file name (Including the path) and see why it is not matching..
Yes it's fine.. if you want to search for files inside E:\Testing.Old then you can use
Get-ChildItem "E:\Testing.old" -Recurse
@Subsun

Thats brilliant thanks!

I have worked out where the problem was. (The file extension for the files).

I will set this to solved and give you the points now.

Thanks again for the help!