Answered by:
Extract firstname from full name using powershell

Question
-
I have a csv file with fullnames of users, I am trying to extract the first name of the users using powershell, however it is not working:
$users = Import-Csv C:\test.csv | Out-String
foreach ( $User in $Users) {
$fname=$User.Trim() }
$fname
Any suggestions?
Wednesday, January 2, 2019 8:35 AM
Answers
-
File is like this:
FullName
James Simon Fred White Ravi Kumar
Import-Csv c:\test.csv | ForEach-Object{ ($_.Fullname -split ' ')[0] }
\_(ツ)_/
Wednesday, January 2, 2019 12:18 PM
All replies
-
It depends pretty much on what's in your CSV file .... could you show a few lines of it? The string method .Trim() only removes leading or trailing white spaces.
Regardless of that: Please format your code as code here in the forum. Use the code posting tool provided on the edit bar of the post editor. Thanks.
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Wednesday, January 2, 2019 9:29 AM -
Without knowing the format of the names, i have provided a couple examples of using split instead of trim. this should get you what you are looking to achieve.
Write-Host "Show split with a Space" $user = 'homer simpson' $fname = $User.split() Write-Host "My First name is: " $fname[0] Write-Host "My Last Name is: " $fname[1] Write-Host "" Write-Host "" Write-Host "Now show split with a Comma" $user = 'simpson,homer' $fname = $User.split(",") Write-Host "My First name is: " $fname[1] Write-Host "My Last Name is: " $fname[0]
- Edited by Lance Newingham Wednesday, January 2, 2019 9:37 AM changed to code editor
- Proposed as answer by Richard MuellerMVP Wednesday, January 2, 2019 10:42 AM
Wednesday, January 2, 2019 9:29 AM -
Please format your code as code here in the forum. Use the code posting tool provided on the edit bar of the post editor. Thanks.
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Wednesday, January 2, 2019 9:30 AM -
File is like this:
FullName
James Simon Fred White Ravi Kumar
Wednesday, January 2, 2019 11:02 AM -
Hey Admin
You can try the given code
Import-Module ActiveDirectory $UserList = Get-Content 'c:\temp\AllUsersSamaccountname.txt' Get-ADUser -Filter '*' -Properties MemberOf, WhenCreated | Where-Object { $UserList -contains $_.SamAccountName } | Select-Object GivenName, Surname, SamAccountName, WhenCreated, @{ Name = 'Groups' Expression = { ( $_.MemberOf | ForEach-Object { (Get-ADGroup $_).Name } ) -join ', ' } } | Export-Csv -Path 'c:\temp\AllUserinfo6.CSV' -NoTypeInformation
mark it as answer if found useful
Wednesday, January 2, 2019 11:21 AM -
File is like this:
FullName
James Simon Fred White Ravi Kumar
Import-Csv c:\test.csv | ForEach-Object{ ($_.Fullname -split ' ')[0] }
\_(ツ)_/
Wednesday, January 2, 2019 12:18 PM -
Thanks that works. One last question. If I want to take the first name and create a column called FirstName in the same csv file and add all the firstnames in the same csv file so that it looks something like this, how can I do it:
Fullname,FirstName
James Simon,James
Fred White,Fred
Ravi Kumar,Ravi
Wednesday, January 2, 2019 12:54 PM -
You really need to start a new topic for a new question.
I recommend studying PowerShell and how to use it with the base commands and programmatic elements. You are asking us to write your script one line at a time. Learning PowerShell will help you the most.
Import-Csv c:\test.csv | select Fullname, Firstname | ForEach-Object{ $_.FirstName = ($_.Fullname -split ' ')[0] } | Export-Cav c:\newtest.csv -NoType
\_(ツ)_/
- Proposed as answer by BOfH-666 Wednesday, January 2, 2019 4:47 PM
Wednesday, January 2, 2019 1:05 PM -
$users = Import-Csv -delimiter " "
Chris
Wednesday, January 2, 2019 5:15 PM -
$users = Import-Csv -delimiter " "
Chris
Won't work correctly. Add a header and it can work.
\_(ツ)_/
Wednesday, January 2, 2019 5:17 PM -
This is not working. I know how to export to a new csv file. What I was actually looking for is to append the firstnames to the original csv file.
You really need to start a new topic for a new question.
I recommend studying PowerShell and how to use it with the base commands and programmatic elements. You are asking us to write your script one line at a time. Learning PowerShell will help you the most.
Import-Csv c:\test.csv | select Fullname, Firstname | ForEach-Object{ $_.FirstName = ($_.Fullname -split ' ')[0] } | Export-Cav c:\newtest.csv -NoType
\_(ツ)_/
Wednesday, January 2, 2019 11:39 PM -
You cannot read and write to the same file at the same time. And there is no native Powershell cmdlet to add an additional column to a CSV file. So you will have to create a new CSV file first, delete the old one and rename the new one with the old name.
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Wednesday, January 2, 2019 11:45 PM -
You cannot read and write to the same file at the same time. And there is no native Powershell cmdlet to add an additional column to a CSV file. So you will have to create a new CSV file first, delete the old one and rename the new one with the old name.
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
In PowerShell There is no way to add fields to an existing CSV. The file must be recreated. TO use the same file name we can do this.
(Import-Csv c:\test.csv) | Select-Object Fullname, Firstname | ForEach-Object{ $_.FirstName = ($_.Fullname -split ' ')[0] } | Export-Cav c:\test.csv -NoType
\_(ツ)_/
- Proposed as answer by BOfH-666 Wednesday, January 2, 2019 11:53 PM
Wednesday, January 2, 2019 11:48 PM -
The below script creates a blank csv with no contents:
(Import-Csv c:\test.csv) | Select-Object Fullname, Firstname | ForEach-Object{ $_.FirstName = ($_.Fullname -split ' ')[0] } | Export-Csv c:\output.csv -NoType
Thursday, January 3, 2019 12:10 AM