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 later, you don’t need to download any extra files, just: ‘Add Feature’ –> Windows PowerShell.  However, for older operating systems, 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 or later, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save 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 1:  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 2:  I recommend launching Excel and using it open the file to check if exporting achieved what you expected.

Note 3:  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 4: #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 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 and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

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
}

Note 5: You will need to experiment with value of the variable $FilePath.

Research Import-CSV Parameters

PowerShell has its own built-in help, you access the information via Get-Help thus:

# 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. Append the -Full parameter to see the examples.  Get-Help Import-CSV -Full.

Free CSVDE Import Tool »

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

SolarWinds’ Network 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.

What I like best is the way NPM suggests solutions to network problems.  Its also has 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 try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Import-CSV Has an Alias Called 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 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.

See more about ConvertFromCSV »

Summary of PowerShell Import-CSV Cmdlet

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   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

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.