powershellcsvfilenamesfile-ownership

Import .csv to create a list of filenames and corresponding owners


I am working on creating a script that will read a .csv document containing a single column of filenames (one per cell) and search a larger folder for each of the files matching the filenames provided and identify the 'owner' using:

(get-acl $file).owner

Currently I have several bits of code that can do individual parts, but I am having a hard time tying it all together. Ideally, a user can simply input file names into the .csv file, then run the script to output a second .csv or .txt identifying each file name and it's owner.

csv formatting will appear as below (ASINs is header):

ASINs
B01M8N1D83.MAIN.PC_410
B01M14G0JV.MAIN.PC_410

Pull file names without header:

$images = Get-Content \\path\ASINs.csv | Select -skip 1

Find images in larger folder to pull full filename/path (not working):

ForEach($image in $images) {

    $images.FullName | ForEach-Object

       {
       $ASIN | Get-ChildItem -Path $serverPath -Filter *.jpg -Recurse -ErrorAction SilentlyContinue -Force | Set-Content \\path\FullNames.csv
       }
}

At that point I would like to use the full file paths provided by FullNames.csv to pull the owners from the files in their native location using the above mentioned:

(get-acl $file).owner

Does anyone have any ideas how to tie these together into one fluid script?

EDIT I was able to get the following to work without the loop, reading one of the filenames, but I need it to loop as there are multiple filenames.

New CSV Format:

    BaseName
    B01LVVLSCM.MAIN.PC_410
    B01LVY65AN.MAIN.PC_410
    B01MAXORH6.MAIN.PC_410
    B01MTGEMEE.MAIN.PC_410

New Script:

$desktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
$images = $desktopPath + '\Get_Owner'
Get-ChildItem -Path $images | Select BaseName | Export-Csv $desktopPath`\Filenames.csv -NoTypeInformation
$serverPath = 'C:\Users\tuggleg\Desktop\Archive'
$files = Import-Csv -Path $desktopPath`\Filenames.csv
While($true) {
ForEach ($fileName in $files.BaseName)
{
Get-ChildItem -Path $serverPath -Filter "*$fileName*" -Recurse -ErrorAction 'SilentlyContinue' |
        Select-Object -Property @{
            Name='Owner'
            Expression={(Get-Acl -Path $_.FullName).Owner}
          },'*' |
        Export-Csv -Path $desktopPath`\Owners.csv -NoTypeInformation
}
}

Any ideas on the loop issue? Thanks everyone!


Solution

  • This example assumes your csv contains partial filenames. It will search the filepath and filter for those partials.

    Example.csv

    "ASINs"
    "B01M8N1D83.MAIN.PC_410"
    "B01M14G0JV.MAIN.PC_410"
    

    Code.ps1

    $Files = Import-Csv -Path '.\Example.csv'
    
    ForEach ($FileName in $Files.ASINs)
    {
        Get-ChildItem -Path $serverPath -Filter "*$FileName*" -Recurse -ErrorAction 'SilentlyContinue' |
            Select-Object -Property @{
                Name='Owner'
                Expression={(Get-Acl -Path $_.FullName).Owner}
              },'*' |
            Export-Csv -Path '\\path\FullNames.csv' -NoTypeInformation
    }