Solved

Powershell - Pulling a single users info from a .csv

Posted on 2016-11-03
5
18 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 82

Accepted Solution

by:
oBdA earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
$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 82

Assisted Solution

by:oBdA
oBdA earned 200 total points
Comment Utility
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
Comment Utility
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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
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.…
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now