Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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'
}

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading