Listing file information filtered on values in a file

Fredrik M 206 Reputation points
2024-06-26T09:18:57.3433333+00:00

Hi,

I would like to list sizes on all files in a directory, but the listed file names must match filenames picked up from a file.

Basic information file size from this;

Get-ChildItem -Path "\servername\DATA\files" -File | Select-Object Name, Length | Export-Csv -Path "C:\Temp\FileSizeResult.txt" -NoTypeInformation

But the files I am picking up should match the file names I have listed in the file [FileNames.txt], looking like this:
File2024.txt
File2025.txt
File2026.txt
...and so on.

Any help from here?
Thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,443 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,255 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,591 Reputation points
    2024-06-26T15:42:02.9966667+00:00

    @MotoX80 Hmmmm . . . I think that your answer (which works) is one where the use of a pipeline isn't warranted. While the number of files to be examined is probably pretty small using the pipeline won't have any noticeable effect on runtime, the use of the Where-Object isn't adding anything since the Get-ChildItem is going to get the name of the file from the pipelined Get-Content.

    I suppose this might be a matter of coding style, though.

    Here's another way to get the same results:

    $Files = get-content .\test.txt
    $Files  # Just to show the files to be examined. Remove this if you don't want to see that.
    Foreach ($File in $Files){
        Select-Object -InputObject (get-childitem $File) -Property Name, Length
    }
    

    Note: If you don't want to show the contents of the file .\test.txt you make the code even shorter:

    Foreach ($File in (get-content .\test.txt)){
        Select-Object -InputObject (get-childitem $File) -Property Name, Length
    }
    

1 additional answer

Sort by: Most helpful
  1. MotoX80 32,551 Reputation points
    2024-06-26T13:23:47.0733333+00:00

    Try something like this. (Version 2.0)

    $Files = get-content .\test.txt 
    get-childitem  | where-object -Property Name -in $Files  | Select-Object Name, Length
    
    
    
    0 comments No comments