Solved

Powershell - Pulling a single users info from a .csv

Posted on 2016-11-03
5
24 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 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 13

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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).
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this Micro Tutorial viewers will learn how to use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

739 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