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:
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
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}
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
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
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?
ASKER
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
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
Replace Get-ChildItem -Recurse with Get-ChildItem E:\ -Recurse
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
@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
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?
ASKER
@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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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
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
ASKER
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
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
Get-ChildItem "E:\Testing.old" -Recurse
ASKER
@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!
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!
Open in new window