How to Import from csv (multiple rows and columns) as variables? RRS feed

  • Question

  • Forgive the fact this may be a noob question, but I've been a networking guy all my life and am trying to learn some PS and need to see some correct syntax for what I'm trying. Also, thanks in advance for all help, I certainly appreciate it. On to the problem:

    I'm currently using batch files to do some basic, albeit repetitive, tasks and thinking I can use PS to make it more efficient. If I wanted to do something simple like make a directory on multiple different network attached PCs, on multiple different user profiles, but input those items as variables so I can call on it once instead of typing it out in each batch script I write. So basically I want it to look similar to:

    import-csv -path C:\directory


    mkdir "\\$PCName1\users\$Username1\NewFolder" mkdir "\\$PCName2\users\$Username2\NewFolder" mkdir "\\$PCName3\users\$Username3\NewFolder" mkdir "\\$PCName4\users\$Username4\NewFolder"

    where I just put all the PC names into column 1 and all the user names into column 2? This isn't exactly what I'm trying to accomplish, but its a good jumping off point if I could make this work, where I could tweak what I have already and do multiple actions without changing a bunch of batch variables over and over. Again, many thanks for taking the time to read and help.

    Tuesday, November 5, 2019 8:51 PM

All replies

  • simple. Just read the help for the commands and the exam0les will show you how to do this.

    help import-csv -online

    help New-Item -online

    help foreach-object -online

    Always start with the help and the documentation.


    Tuesday, November 5, 2019 8:56 PM
  • Thanks...thing is right now I don't even know which commands to check help for, so a jumping off point is good.
    Tuesday, November 5, 2019 8:58 PM
  • Assuming your CSV has column headers named "PCName" and "UserName", this should get you started:

    Import-CSV c:\Junk\FileName.csv |
        Foreach {
           mkdir "\\$($_.PCName)\users\$($_UserName)\NewFolder"

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, November 5, 2019 9:32 PM