0

I have a csv with a list of usernames

I want to import just one cell of the csv file e.g. A2

Is it possible to be that specific? I have tried googling for this but don't see an exact solution. Tried powershell help also.

Can this be done ?

Thanks

Confuseis

3 Answers 3

2

When you use Import-Csv you convert the content into a PSCustomObject.

Examples on the following table:

PS> $csv = Import-Csv .\test.csv
PS> $csv

ProcessName     Id    WS        CPU
-----------     --    --        ---
sihost          5996  30015488  44.640625
pia_nw          11064 10620928  52.921875
pia_nw          2344  7933952   104.0625
RuntimeBroker   6500  77500416  177.34375
SettingSyncHost 6736  5074944   202.796875
explorer        6600  284934144 272.140625
ipoint          920   3162112   372.78125
rubyw           10648 18026496  389.46875
pia_nw          3108  31330304  1640.5625
OneDrive        10208 33206272  6422.4375

So you will need a NoteProperty name to call a value you're looking for.

PS> $csv.ProcessName[0]
sihost

Another way is to make a header array and use that to slice the data.

If working with a an object:

PS> $header = ($csv | ConvertTo-Csv -NoTypeInfo)[0] -replace '"' -split ",";
>>
PS> $header
ProcessName
Id
WS
CPU

Or if working with the file:

PS> $header = (gc .\test.csv)[0] -replace '"' -split ',';
ProcessName
Id
WS
CPU

Then just use the appropriate index:

PS> $csv[0]."$($header[0])"
sihost

Finally there is the Excel.Application ComObject method on an xlsx file. This will let you select cell's and ranges.

PS> $file = "C:\Some\Path\IMade\Up\test.xlsx"
PS> $objExcel = New-Object -ComObject Excel.Application
PS> $objExcel.Visible = $false
PS> $wb = $objExcel.Workbooks.Open($file)
PS> $ws = $wb.Sheets.Item(1)
PS> $ws.Range("A2").Text
sihost

More info on using the ComObjects can be found here: Application Object (Excel)

Sign up to request clarification or add additional context in comments.

1 Comment

Thats very helpful, Much easier thatn I thought it would be Thanks
1

The below example will select and output only 'cell' A2 from test.csv

In this example, the column header for row A is 'username'

$inFile = Import-Csv c:\Temp\test.csv
$targetCell = $inFile.username[1]
Write-Output $targetCell

This snippet is doing the following:

  1. Import the csv file, yielding a PowerShell object.
  2. Select the column you want to work with, the items from that column can be treated as an array. Select the desired item in that column by referring to it's zero based index value.
  3. Output the results.

2 Comments

You put the index on the username property, not the array. $inFile[1].username
Actually, either way works. $infile.username[1] or $infile[1].username give the same value.
1

Import-CSV creates an array of objects from the input file. The column labels in the first row of the CSV become the property names. The other rows are objects in the array. Like any array you can call one element using brackets.

$arrUsers = Import-CSV c:\temp\users.csv
$arrUsers[1] 

The second command, above, prints the second object, since counting starts with 0. This object came from the third line of the CSV file, since the first was used as column headers.

If you use Get-Member, it will show you the members (properties and methods) of an object.

$arrUsers | Get-Member

Assuming one of the members is username, combine this with array indexing, you can use:

$arrUsers[1].username

Import-CSV is a very flexible tool. Especially combined with Foreach and Export-CSV. Between Get-Help and Get-Member, you can explore Powershell with ease. Good luck.

1 Comment

Thats perfect Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.