Creating AD Users from CSV with Powershell | ServerAcademy.com

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi it's Paul Hill from it fleet.com and in this lesson I'm going to be showing you how you can create user accounts with Powershell for active directory based off an Excel spreadsheet or any comma separated value spreadsheet okay it's not just Excel it can work with anything that you can export to a CSV file type now what you're looking at here is a list of 10 users and it contains information about like their job title office phone email address Etc it also contains the destination organizational unit of where this user is going to go now every once in a while you'll get a list like this from your boss and it'll say I need a new uh you know these user accounts need to be created and they need to be put in the appropriate OS and we need it done you know in a day or in two days or something like that or sometimes maybe in an hour and it would take a lot of time to go through active directory create a new user account place them in the appropriate OU enter in this description their email address phone number job title and all that and then doing that with the guey for all 10 of these users now what if imagine if you had about 75 or 100 people that you had to create I once worked in an office space where we had classrooms and all the new students needed user accounts and this was about classrooms of about 40 to 50 people and it would cycle every two weeks so I was constantly creating new user accounts for all the new students who would be coming in and I can tell you for a fact that this is a pain so you can script this in Powershell and make your life much easier so a couple things to keep in mind is that we're not going to be saving this as an Excel spreadsheet file we're going to be saving this as a CSV in Excel you hit file you hit save as and then you choose under the type here you choose comma separated values or comma delimited you can add more information if you'd like I be showing you how to uh you know add in different columns if you'd like to uh for example you could include something you know other than the description or or phone number email address so if you go to techet and you look at the new ad user here's a list of everything that you can specify an active directory you can specify the city the company they work for their County you know their uh division employee ID fax number you know uh all this information you can specify if you would like but I just stuck with the Bare Basics first name last name uh job title office email address description and the organizational unit now obviously you're not going to be given this OU uh right off the bat when you know a hiring manager or someone is telling you you need these new accounts this is something you're going to have to get your and that's pretty easy to do like I showed you if you haven't watched my other video about creating users in active directory with Powershell I recommend you do that now but I'll just show it to you real quick first in active directory I have the console open here and I'm going to enable Advanced features and I have an OU structure here called it flea and then I have administrators and I have office so if I right click on one of these OS and I choose properties and I go to attribute editor the distinguished name is the OU path that we're going to be looking at so that's this option right here okay so what we're going to do now is start by understanding how to edit or how to read this CSV file in Powershell so I'm going to close out of this Excel spreadsheet and I'm going to minimize this and we're going to open the Powershell isse so the first thing we're going to do is when we're working with active directories we need to import the required modules so we're going to say that in a comment and we're going to call the first line that we're going to write is import module and and we're going to import active directory so all in word okay the next thing we're going to do is get the file path from the user so we're going to ask them to enter the destination or where the file is located that we're trying to open and trying to read so we'll say prompt user for CSV file path okay and we're going to assign this to a variable called file path so file path is equal to and if you remember the read host command is the command that allows us to get user in put and store in a variable now we're going to add a prompt and we're going to say please enter the path to your CSV file and we'll hit end quotation marks all right now we're going to load that file into a variable and we're going to call this variable users so let's just call this uh this comment we're going to say we're going to import the file into a variable okay and this variable is going to be called users we're going to say user is equal to we're going to use the command import-csv and then the CSV file that we're going to specify is going to be the file path so we're importing active directory we're getting the file path from the user by reading the host and then once we have that file path we're going to import the CSV into this variable called users all right so let's go ahead and just give this a test make sure we don't have any errors in our code I'm going hit the play button and we are going to Pro be prompted to enter the path to our CSV file now let's open Windows Explorer and I have mine on the C drive you can find this file under the resources section of this lecture as well as that link to the TechNet article that lists all the arguments and everything you can do with this command so under resources you'll find this new users. CSV we're going to right click on this file and choose edit and we can see this is the same exact information we're looking at in Excel so we have first name at the top last name job title Etc and then this is the list of users so it's under the C drive and then it's new users. CSV so I'm going to type in capital c colon back slne users. CSV and we'll press enter and okay so we didn't get any error so that's good all right the next thing we need to do and this is really simple I mean we're we're literally almost done with this code now we need to Loop through each column or each row of the CSV file so if I pop this open I'll explain we're going to use a for each command and what that will do is once we import this CSV file this first section or this first line will be considered a header next it's going to go and loop through each of these lines and we're going to be able to call out particular information from each line like the first name or the last name and we'll be able to store all that information so we can write one line of code to create a new one new user account and apply it to each of these lines it'll make more sense in a second so let's just go ahead and start and we're going to call this this uh comment is going to say Loop through each row and gather information okay so again we're going to use a command for each and now we have to specify what we're going to call each variable so we're going to say for each user in users and that's also a variable and then we're going to do an opening bracket and a close bracket okay all right so let me expl what's going on so each one of these lines let me pull this over to the side so we can see what's going on here all right each one of these lines is going to be considered uh a user so in the file users which is this whole file we're going to have a user and each user will be like one of these lines so we're going to specify a certain amount of actions that need to be completed for each user and it's going to Loop down just like this so it's going to go down the file oops one line at a time and we're going to be able to grab the first name last name of each user Etc Al so all the information we're specifying we're going to be able to grab all of that and it's going to do it all one at a time okay so let's start by just figuring out how do we get each column so we first thing we're going to start with is the first name because that's the first thing in this document so we want to start by grabbing this information and the way we do that is first make a comment and call it gather the information we'll say gather the users information okay and we're going to come up line and we're going to say we're going to go with our normal variables like we've been doing before we're going to call it f name or first name and we're going to say is equal to we're going to call this variable at the top here so is equal to user because we're going to a particular line okay and then we're going to say dot and we're going to say and we can see right here it's it's being listed for us so if we say dot first name and press enter and now we can see that if we Echo this out this variable F name and hit run we're going to see that it's going to Echo the first name of every user go starting from mik and ending with Ernest all right so let's give it a shot HP play and we're again C drive backnew users. CSV and here in the output we can see let me pull this up a little bit we can see that we started with Mike here at the top and we ended with Ernest so it's going through each line and it's outputting the first name okay so you can kind of see how we can grab each one of these variables and we can create a new user account all right because it's completing this this line this little block of code for each line in the CSV file all right now let's go ahead and grab more information so we're going to grab the last name or we're going to call it lame okay and that is equal to user Dot and let's go down through this list here and with last name we'll press enter or tab okay now we have job title we'll call this J title equal to user dot job title and this information this job title and description is being automatically populated so any CSV file that you have it will have this information for you if you do not see this information you may need to run the script once and load the file into memory I'm not entirely sure on that one so I'm just going to press enter and also notice that it's putting it in quotation marks if my variables were in uh didn't have a space you would not need the quotation mark So if it was like dot last name if and the CSV file then let me get that right it could be written just like this but since we have a space we need to wrap everything in quotation marks okay all right so next thing we need to get if we look at this document file so we have first name we have the last name we have the job title and then we have the office phone so that's going to be the next thing we're going to grab we'll call this office phone equals user. office phone okay and we can just Echo out this input if we'd like so let's just do that and check what we've got going on here we'll do job title J title sorry and then office phone okay now let's just hit play and see what happens so I'm going to type in the C drive new new users. CSV I'm not sure if this is Cap sensitive it's not okay so we can see that it's outputting the information so we have starting with Mike Terry it's oops let's go starting with Mike Terry we have the office manager and his phone number okay so we need to keep going on down the next one is going to be the email address equals user. email address and if you're getting a little bit confused about what I'm opening I'll just pop this open and show you in the spreadsheet that new text document might be a little bit confusing if I bring this out I'll bold this first line for us so it's going through uh each one of these lines over here and we just need to call out these top little headers and that's all we're doing right now so we just did email address the next one we're going to do is do description okay so minimize that okay so now we have description equals user. description and you can see it didn't wrap it in quotation marks because we do not have any spaces in the header okay or in that heading all right so now we have the OU path that's the very last variable we need to get so OU paath equals user. organizational unit okay all right so now we have all the users now believe it or not all we need to do is add the new aduser command and then we're going to change this echo down here so I'm going to change this to account created for f name L name and OU path okay so once we create the user account it's going to say that hey we've created the user account for you know Mike Terry and this following OU paath and we'll put a comment in there and just say Echo output for each new user okay we'll go down a couple lines and now we're going to make this line create the ad user create new sorry new ad user for each user in CSV file okay all right so this command again if you remember is new- auser and the first argument is going to be the name and that's going to be oops let's get this some quotation marks F name space l name in quotation marks and then we got to go with a given name that's going to be fname then we have the surname oops hitting all kinds of wrong buttons here there we go surname and that'll be L name all right now we have the user principal name which is their user account name and this will be in quot in quotation marks it'll be fname oops fname do L name all right now we need to specify the path and this is going to be the OU path OU path and account password and that is going to be secure password which I don't think we have actually created yet so we're going to have to make sure we go up and do that last all right now change password at log on this is totally up to you I'm going to say uh true because anytime I make a new account you know I want them to change the password we've talked about that before okay office phone number is going to be office phone and you notice I'm using the tab completion a lot that's really really important that you keep using this uh tab completion it keeps you from making mistakes and stuff like that okay so now we're going to do the description and that'll be description and enabled equals true that's the last thing we need to do okay okay and it looks like I didn't add the email address so I'm just going to go back here and make sure I add the email address it doesn't matter what order you do this stuff in as long as you get it in there so we're just going to say email address and now we are good to go so now we need to go back and create the password and then we're done with this one so we're we'll be ready to test it at least so I'm going to go up here and uh create a new password all right now you could do you could have your you know promptly administrator type in a new password for them I'm not going to do that uh because it's just too much work you could also have the user put in create a new password for each individual user in this for Loop so if I created a section here saying you know read host please enter the new password every time I'd have to enter a new password for every new user that was created you could also randomly generate the password and uh output that in clear text so that it'd be different for everyone if you would like uh but I'm not going to do any of that I'm just going to give everyone the same password uh just to keep things simple so we're going to call this secure password and that's going to be equal to convert to- secure string and we're going to say test use the same old same old pass word with a zero and exclamation mark we're going to say as plain text and force okay so now we're ready to save this command so we'll hit or save this code so let's save this to the desktop and call it create users from CSV not a very original name but that's fine so I'm going to hit save and we'll close out of this and let's just minimize out of these windows we have open here and let's right click and run with Powershell okay Enter the path to our CSV file and we're going to say new users. CSV press enter okay so we can see that it created a bunch of accounts if we hit F5 in these o us now we can see that the new accounts have been created so they have their description if we open up one of these we can see they have their email address populated their phone number their description and all that good stuff they have their username in here and we are good to go so if you want to download the script you don't want to write it yourself it's under the attached resources uh I hope you guys find this uh interesting it definitely is interesting for me I love doing stuff like this yeah so I encourage you guys to go ahead and play around with the script for example try making the user input a password before creating you know a password for each user and also you can go inside this for Loop and generate a random password for the user uh that would be kind of cool and then you can output it in clear text uh so that you can read it and then you would need to pause it before the command ended so down here you'd want to say pause but I recommend that you play around with this add more information from that TechNet website that I sent you you could do employee ID uh you can you know change this uh homepage home phone uh I guess we already have that in there but you can change the office you know other name all that stuff so I recommend that you go through here find some of these other fields and add them in yourself so that you get a better understanding of what you're doing uh it's really fun to just play around with this stuff it's can come in super handy when you have you know a ton of people that you have to create accounts for but I think you have a basic understanding of how you call a header so you should be able to take your own Excel spreadsheets and just you know create scripts based off them it'll really come in handy later on and that's all we need to do in this lesson sorry I know this lesson was a lot longer than what I normally make but I hope you found the information useful and I hope you enjoyed it I certainly enjoyed making the lesson for you and I'm looking forward to making more like this so I look for to seeing you in the next lesson
Info
Channel: Server Academy
Views: 116,824
Rating: undefined out of 5
Keywords: ITFlee, itflee.com, paul hill, Powershell, New-ADUser, CSV, Automatically create AD users with Powershell, Creating AD users with PowerShell, Import users from CSV to Active Directory, Active Directory`
Id: 9WAcQE-Q9xo
Channel Id: undefined
Length: 18min 32sec (1112 seconds)
Published: Sun May 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.