Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell - Pulling a single users info from a .csv

Posted on 2016-11-03
5
Medium Priority
?
31 Views
Last Modified: 2016-11-06
I am creating some AD and MSOL users from a CSV using powershell. The users get created fine using ‘import’ and then ‘for each’ commands.

----------------------------------------------------

$UserCSV = Import-Csv -Path "C:\Path.csv"
       
    foreach ($User in $UserCSV)            
    {            
        $FirstName = $User.Firstname        
        $LastName = $User.Lastname
        $UserName = $User.Username.ToUpper()
        $Password = $User.Password
               
    # Create User Commands          
    New-ADUser -Name $UserName etc etc
    }

--------------------------------------

But then once the users are created I want to sync them to MSOL, and as this tends to take a while check one of the users exists before moving on to the next lot of commands.

How can I pull just one of the users details from the .csv and set variables based on that single users info?
ie:
$Username = ‘The last user in the .csv username’
$emailaddress = ‘The last user in the .csv email address’
0
Comment
Question by:Pete
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 800 total points
ID: 41872472
Depends on how exactly you want to identify the user from the csv.
You can get the last object in the file like this:
$LastUser = $UserCSV | Select-Object -Last 1

Open in new window

0
 
LVL 13

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 400 total points
ID: 41872475
You can use the .Length property of your array to see how many items are in it (example):
$users = Import-CSV C:\test\user.csv

$howmany = $users.Length

Write-Host $howmany

Open in new window


Then you can use $users[$i] to call the item in the list.  Here is a loop that does just that:
$users = Import-CSV C:\test\user.csv

for ($i = 0; $i -lt $users.Length; $i++)
{
    Write-Host $users[$i].username    
}

Open in new window


(sample csv attached)
user.csv
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 800 total points
ID: 41872483
$UserCSV[0]  will give you the first item in variable.. $UserCSV[-1] will output last item.
Or
$UserCSV = (Import-Csv -Path "C:\Path.csv")[-1]

You can also use Select-Object
$UserCSV | Select -First 1
or
$UserCSV | Select -Last 1

Open in new window

0
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 800 total points
ID: 41872490
Come to think of it: in case you want to select just any old user from the csv, with a bit of comfort, you can do that like this:
$SelectedUser = $UserCsv | Out-GridView -Title 'Select a user from the CSV' -OutputMode Single
#  Do something with the selected user object ...
$SelectedUser

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 800 total points
ID: 41872543
But then once the users are created I want to sync them to MSOL, and as this tends to take a while check one of the users exists before moving on to the next lot of commands.
You can add a If condition to check if user status and then continue based on that.. Example..
New-ADUser -Name $UserName etc etc
#wait for 60 seconds..
Sleep 60
#Get UserPrincipalName from csv if you are using Get-MsolUser to check user status

$UserPrincipalName = $User.UserPrincipalName

#User check here - Use Get-MsolUser or Get-ADUser based on your requirement

$User = Try {Get-MsolUser -UserPrincipalName $UserPrincipalName}Catch{}

If ($User -ne $null){
 # Do some thing
}

Open in new window


Or you can add sleep until the user is created..
Example..
Do {Sleep 10;$User = Try {Get-MsolUser -UserPrincipalName $UserPrincipalName}Catch{}} Until ($User)

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
In this Micro Tutorial viewers will learn how they can get their files copied out from their unbootable system without need to use recovery services. As an example non-bootable Windows 2012R2 installation is used which has boot problems.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question