Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Windows PowerShell Import-CSV

Windows PowerShell Import-CSV Cmdlet

The Import-CSV cmdlet is often employed for changing the format of the data.  Another use is for creating users by bulk importing their data from a csv file.

Windows PowerShell Import-CSV Topics

 ♣

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and Server 2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.  However, for older operating systems, installing can be confusing because there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft's site.

Once you have installed PowerShell 2.0, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save you buying a text editor.

Example 1: PowerShell Import-CSV

Simple Scenario: We want to examine the properties of Windows services.  The technique is to first export the data, then import and finally, apply Get-Member.

Stage A: Export Only

This is in preparation for the Import-CSV example.

# PowerShell Export-CSV Example
Clear-Host
$FilePath = "E:\PowerShell\Process\Serv4s.csv"
Get-Service | Export-CSV $FilePath

Note:  One reason that I have used a variable to control the path, is that I want to remind you to change its value before you run this script on your machine.

Note:  I recommend launching Excel and using it open the file to check if exporting achieved what you expected.

Note:  Save Frustration
You cannot use Format-Table with the Export-CSV cmdlet.  However you could use Select-Object to choose just the properties or columns that interest you.  See Example 2.

Excel Spreadsheet Screenshot

PowerShell Import-CSV

Note: #Type System.ServiceProcess in the first row is the .NET Framework type of the object.

Stage B: Actual Import

# PowerShell Import-CSV to View Properties
Clear-Host
$FilePath = "E:\PowerShell\Process\Serv4s.csv"
Import-CSV $FilePath  | Get-Member

This assumes that you have a file called Serv4s.csv.  If necessary check with Stage A: above.

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Example 2: ForEach with Import-CSV

This is a clumsy example, its sole purpose is to show how you can extract data with a foreach loop.  I hope that it gives you ideas for your project.  As ever, do send me a better example if you have one.

# PowerShell Import-CSV Example
Clear-Host
$FilePath = "E:\PowerShell\Process\Serv4s.csv"
$Stuff = Import-CSV $FilePath
ForEach ($Victim in $Stuff) {
Write-Host $Name =$Victim.Name `n
}

Research Import-CSV Parameters

# PowerShell Import-CSV Parameters
Clear-Host
Get-Help Import-CSV

Checking the help file may reveal useful parameters, for instance you can adjust the delimiter from the default comma to the semicolon.

Export-CSV Alias IpCsv

With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner.  One obvious example is that you can abbreviate format-Table to ft.  As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).

Researching Similar PowerShell Cmdlets

# PowerShell CSV Cmdlet Research
Clear-Host
Get-Command -noun CSV

As we know, there is a Export-CSV cmdlet, but you may not realize there is a ConvertTo-CSV cmdlet.  PowerShell -noun or -verb research always throws up at least one surprise.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

Solarwinds' Orion performance monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is the ability to monitor the health of individual VMWare virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer.

Download a free trial of the Network Performance Monitor.

Researching Similar PowerShell Cmdlets

# PowerShell CSV Cmdlet Research
Clear-Host
Get-Command -noun CSV

As expected there is an Export-CSV cmdlet, but you may not realize there is a convertFrom-Csv cmdlet.  PowerShell -noun or -verb research always throws up at least one surprise.

Summary of PowerShell Import-CSV

Import-CSV works hand-in-glove with Export-CSV.  Possible usage for this cmdlet include manipulating formats and bulk-import of users into Active Directory.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home  • Out-File  • Out-GridView  • -f format  • Files  • Pipe to file

Export-CSV  • Import-CSV  • Format-Table  • Format-List  • Read-Host  • Write-Host

ConvertTo-Html   • Windows PowerShell   • PowerShell 3.0 Redirection

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.