TOGEEK
TOGEEK

Reputation: 741

Powershell Import-CSV outputs "@{"

relative newbie on PS. I have a simple CSV file:

Computer,Team
server,team1
server2,team2
server3,team2

and I wish to import this list into PS. I use the following command, which I will use to perform queries against each computer name:

$Serverlist = import-csv "c:\temp\appsupportAll.txt"
$Servers = $Serverlist | Select Computer
Foreach ($Computer in $Servers)
    {etc etc}

The output of $Serverlist and $Servers is as I expect it. However, each $Computer is presented to PS as:

@{$_.Computer=server}
@{$_.Computer=server2}
@{$_.Computer=server3}
etc

I am looking for "server", "server2", "server3", etc. What am I doing wrong please?

Thanks

Upvotes: 4

Views: 1370

Answers (2)

Fabio Ramirez
Fabio Ramirez

Reputation: 11

If your input file does not have headers, use this instead:

 $Serverlist = Get-Content "c:\temp\appsupportAll.txt"

 $Serverlist | % {whatever you need}

Upvotes: 0

Dave Markle
Dave Markle

Reputation: 97861

$Computer inside your foreach loop is not just the value you're looking for. It is, in essence, a custom object with one property called "Computer". If you just want the string representation of the single "Computer" property, use select -ExpandProperty like so:

$Serverlist = import-csv "c:\temp\appsupportAll.txt"
$Servers = $Serverlist | Select -ExpandProperty Computer
Foreach ($Computer in $Servers)
    {etc etc}

or even more simply, you can just write this:

$Serverlist = import-csv "c:\temp\appsupportAll.txt"
Foreach ($Computer in $serverlist.Computer)
    {etc etc}

or, if you need more properties:

$Serverlist = import-csv "c:\temp\appsupportAll.txt"

foreach ($server in $serverlist) {
    $server.team
    $server.computer
}

Upvotes: 3

Related Questions