Powershell – Test-Path with Import-CSV – remove @{Path= when importing

Advertisements

I am creating simple powershell to grab data from a CSV(list of directories) and pass it into Test-Path to check if the path exists. However, the path from the csv is passing in…differently.
$csvdata = Import-Csv "C:\X\testfolder.csv"

foreach($entry in $csvdata){

If(Test-Path "'($entry)'"){
    Write-Host $entry ' Folder exists'
}
Else{
    Write-Host ($entry) ' Folder does not exist'
}

}

test-path "C:\X\testfolder.csv"

This is what I get
@{Path=C:\X} Folder does not exist
@{Path=C:\X\testing} Folder does not exist

Obviously, the issue is the "@{Path=" being passed in. Is there a proper way to grab those rows or should I remove them after the fact? If so, how?
Thank you.

>Solution :

Each $entry object represents an entire row. To get just the value of a particular column, use the column header as a property name, eg.:

Write-Host "Folder '$($entry.Path)' does not exist"

Leave a Reply Cancel reply