locked
Extract values from csv RRS feed

  • Question

  • Hi guys,

    i have these csv, my question is: how can i extract A2,B2,C2 and so on..


    in others words:

    1-  use a foreach 

    2- i want to save A2 value in a variable $computername, B2 value in a variable $localaccount, C2 in a variable  $DomainAccount

    3- then write-host $computername $localaccount  $DomainAccount

    is it possible ?



    • Edited by Ramses147 Friday, May 19, 2017 2:32 AM
    Friday, May 19, 2017 2:30 AM

Answers

  • This is actually quite simple!

    Start by loading in the csv (if you use "," as delimiter, then no need to use -delimiter as "," is the default, if you have any other, then specify it. If you are uncertain open the csv in notepad).

    $input = Import-CSV -path "c:\temp\yourCSV.csv" -delimiter ";" -encoding default

    foreach($row in $input){

    $computerName = $row.computername

    $localAccount = $row."Local account"

    $domainAccount = $row."Domain Account"

    Write-host "$computerName $localAccount $domainAccount"

    }

    I would recommend you watchning the Powershell Jump start on Microsoft Virtual Academy! (mva.microsoft.com)
    It is really good and will get you started with Powershell which is such a great tool for and admin/technician!


    • Edited by Primycha Friday, May 19, 2017 6:42 AM
    • Marked as answer by Ramses147 Friday, May 19, 2017 7:09 AM
    Friday, May 19, 2017 6:38 AM

All replies

  • You need to learn how to use PowerShell with Excel.

    You can learn this by searching for "PowerShell Excel"

    We cannot teach you how to do this.


    \_(ツ)_/

    Friday, May 19, 2017 2:36 AM
  • is not very important for me to use excel, i can use a normal csv, that above is just an example to explain what i'd like to do
    Friday, May 19, 2017 2:48 AM
  • Help Import-CSv -full

    Start by taking a tutorial in PowerShell.


    \_(ツ)_/

    Friday, May 19, 2017 2:54 AM
  • Good tip, thanks for your help
    Friday, May 19, 2017 2:55 AM
  • This is actually quite simple!

    Start by loading in the csv (if you use "," as delimiter, then no need to use -delimiter as "," is the default, if you have any other, then specify it. If you are uncertain open the csv in notepad).

    $input = Import-CSV -path "c:\temp\yourCSV.csv" -delimiter ";" -encoding default

    foreach($row in $input){

    $computerName = $row.computername

    $localAccount = $row."Local account"

    $domainAccount = $row."Domain Account"

    Write-host "$computerName $localAccount $domainAccount"

    }

    I would recommend you watchning the Powershell Jump start on Microsoft Virtual Academy! (mva.microsoft.com)
    It is really good and will get you started with Powershell which is such a great tool for and admin/technician!


    • Edited by Primycha Friday, May 19, 2017 6:42 AM
    • Marked as answer by Ramses147 Friday, May 19, 2017 7:09 AM
    Friday, May 19, 2017 6:38 AM
  • Very good, Primycha !

    maybe better create an array, but thanks !

    Friday, May 19, 2017 7:09 AM
  • Yeah it all depends on what you want to do next, if you just want the information printed out in Powershell I would've just printed the whole $input as is but with this structure you can build on it and do more stuff in the loop.
    Friday, May 19, 2017 7:18 AM