PowerShell ConvertFrom-CSV
The purpose of this page is two-fold, to give working examples of ConvertFrom-CSV and to revive interest in the operating system’s built-in ‘WhoAmI’ command.
Topics for PowerShell’s ConvertFrom-CSV
- Introduction to ConvertFrom-CSV
- Example 1: Simple ConvertFrom-Csv
- Example 2: To Display the Groups Unearthed by WhoAmI
- Example 3: Display User Name for Processes
♣
Introduction to ConvertFrom-CSV
To see ConvertFrom-Csv in action let us create a file using notepad, or better still Excel. In the test file create 3 rows of data with each item separated by the default delimiter, the comma, and then ‘Save as’ using a .txt or .csv extension. Here is an example:
Colour,Ford,Audi,Nissan
Blue,87,37,45
Red,56,21,23
Green,41,32,44
Save the file; call it cars.csv. Make a note of the full pathname, mine was
D: \PShell\cars.csv.
Alternatively, you could call it research.txt, it does not require a csv file extension.
Example 1: Simple ConvertFrom-Csv
The purpose of this preliminary script is simply to check the path to the file containing the list the cars.
# PowerShell Get-Content
$File ="D:\PShell\cars.csv"
Get-Content $File
Note 1: I am expecting the same output, in the same format, as the above example in the yellow box.
Now let us pipe the output into ConvertFrom-Csv:
# PowerShell ConvertFrom-Csv
$File ="D:\PShell\cars.csv"
Get-Content $File | ConvertFrom-Csv
Note 2: The neatest way to view this data is to employ Out-GridView to take care of the formatting. Real-life examples have more rows than this test file, this is where Out-GridView’s ability to filter the data is valuable.
# PowerShell ConvertFrom-Csv
$File ="D:\PShell\cars.csv"
Get-Content $File | ConvertFrom-Csv | Out-GridView
Example 2: To Display the Groups Unearthed by WhoAmI
To see the problem that ConvertFrom-Csv can solve, I suggest that you begin with these commands:
WhoAmI
WhoAmI /Groups
WhoAmI /Groups /FO CSV
Note 3: FO stands for Format, and not something rude!
# PowerShell with WhoAmI and ConvertFrom-Csv
WhoAmI /Groups /FO CSV | ConvertFrom-Csv
Take this opportunity to learn about ConvertFrom-Csv’s parameters, append -header thus:
Clear-Host
$Columns = "Group Name", "Type", "Sid"
WhoAmI /Groups /FO CSV | ConvertFrom-Csv -Header $Columns
Alternatively, employ Out-GridView once more.
# Final code
WhoAmI /Groups /FO CSV | ConvertFrom-Csv | Out-GridView
Guy Recommends: SolarWinds’ Free 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 3: Display User Name for Processes
In this scenario we would like to see who is the owner of a particular process. Such knowledge is useful in eliminating known processes when investigating malware.
Clear-Host
Tasklist /V /FO CSV | ConvertFrom-Csv | Sort-Object "User Name", "Image Name" `
| Format-Table "User Name", "Image Name", "Session Name" -AutoSize
Note 4: I cannot find a way of obtaining "User Name" information using plain Get-Process.
Evaluate Free SolarWinds CSVDE Import Tool ยป
Further Research
We have employed the -Header parameter, but if you want to experiment with other parameters, for example alternative delimiters, then I recommend calling for PowerShell’s help:
#Research parameters and examples
Clear-Host
Get-Help ConvertFrom-Csv -full
Note 5: Help always surprises me, in this case I discovered the -UseCulture parameter to make my scripts more reliable when used in other countries.
Investigate similar PowerShell cmdlets
Clear-Host
Get-Command -Verb Convert*
Summary of PowerShell’s ConvertFrom-Csv Cmdlet
Here we employ PowerShell’s ConvertFrom-Csv cmdlet to solve real problems, such as how to format WhoAmI /Groups, and how to list processes by User Name.
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.