Powershell Read CSV to get File existence status

Joel Heideman 136 Reputation points
2024-02-20T13:20:42.8266667+00:00

I have a script that woks brilliantly to delete files from a CSV. I am struggling to create a script to read the same file to get status that the file no longer exists. Any help? :)

Delete script:

Import-Csv c:\files.csv | Foreach-Object{Remove-Item -LiteralPath $_.FullName -Force}

Validate script

Import-Csv c:\files.csv | get-childitem | where {!$.PSIsContainer} | select-object FullName, LastWriteTime, Length | export-csv -notypeinformation -path verifiedfiles.csv | % {$_.Replace('"','')}

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Rich Matheisen 48,026 Reputation points
    2024-02-20T16:05:55.8633333+00:00

    You aren't very clear about what you want to know. I think what you want is a list files that should have been deleted but were not. If that's the case, this should do it:

    Import-Csv c:\FilesToBeDeleted.csv | 
        ForEach-Object{
            if ( Test-Path -LiteralPath $_.FullName ){	# is the file still present?
                $_ | Select-Object FullName
            } 
        } | Export-Csv -notypeinformation -path FilesThatShouldHaveBeenDeleted.csv
    

    EDIT: Changed "$<underbar>.FullName" to "$_ | Select-Object FullName"


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.