Currently I'm importing a csv file with just one Column named "Name", and I have all the data within the column currently comma separated. When I run the below code example, and then write the out-put I have a weird formatting issue going on which I hope you guys would be able to help. So I could just loop through each name within the array with clean data. Thanks!
$Name = Import-Csv C:\Users\example\documents\testObjects.csv
Here's an example of what the output looks like if I run it through a foreach loop.
foreach ($s in $Name)
{
write-host $s
}
@{Name=abc000100,}
@{Name=def0001000,}
I was hoping for help on how to separate it out so when it run through the foreach loop, it just includes only the name, and not the @{Name=,}. Thank you!
The problem is a CSV file is an object representation. Granted it's generally a flat object but nevertheless an object. The properties of a given object are the column header and the value is the cell value, so to speak.
So, in this case you outputting the whole object, which PowerShell displays in Object/Hash notation. You haven't posted a sample of the CSV file itself, but it sounds like you need just the value of the name property. The following samples would demonstrate how to access it.
$Name =
Import-Csv C:\Users\example\documents\testObjects.csv |
Select-Object -ExpandProperty Name
This should result in a flat array with the property values.
Alternatively you can unroll the property like:
$Name = (Import-Csv C:\Users\example\documents\testObjects.csv).Name
Keeping with your example, and for further demonstration purposes:
$Name = Import-Csv C:\Users\example\documents\testObjects.csv
foreach ($s in $Name)
{
write-host $s.Name
}