Solved

Powershell - Pulling a single users info from a .csv

Posted on 2016-11-03
5
23 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
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 200 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 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 100 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 200 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 84

Assisted Solution

by:oBdA
oBdA earned 200 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 200 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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 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.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

821 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