Home > Software engineering >  Export non-existent file paths to text file from Powershell
Export non-existent file paths to text file from Powershell

Time:12-22

Trying to locate existent OR non-existent directories via powershell and a CSV file with the supposed directories. We can get it to run and display in the console, but it will not export the findings to a txt file. It creates the file, but it is empty. Any help is useful!

Import-Csv .\missingpaths.csv | ForEach {If (Test-Path -Path $_.Path) {Write-Output"$($_.Path) exists" } Else {Write-Output "$($_.Path) does NOT exist" $_.File | Out-File .\MissingCSV.csv -Append }}

CodePudding user response:

The Out-File on your code is on the else statement only (one of he reasons why formatting and indentation is important is because it can help you prevent these issues) unless this was intentional and you only wanted to export those paths that do not exist.

} Else {
    Write-Output "$($_.Path) does NOT exist" $_.File |
    Out-File .\MissingCSV.csv -Append
}

Give this a try:

Import-Csv .\missingpaths.csv | ForEach-Object {
    [pscustomobject]@{
        Path = $_.Path
        TestPath = ('Does not Exist', 'Exist')[(Test-Path $_.Path)]
    }
} | Export-Csv path/to/result.csv -NoTypeInformation
  • Related