How can I supply elements of an array to the New-ADUser Class. Or can I do this another way?

Hi, I want to write a function to create a new user, and I want to keep it as simple as possible.
For example, I want to be able to call the function like.
CreateNewUser John Doe
The function itself will need to pass the value of the name, in this example John Doe into an array. And then I want to be able to reference that array for everything else.
e.g
Function CreateNewUser($fname,$lname)
[string[]]]$objName = $fname,$lname
New-ADUser -name $objname -givenname $objname[0] -surname $objname[1]
I hope you get the idea. Is this possible, am I doing it right, and is there a better way of doing this?
Thanks

If you are just trying to make it look a bit cleaner you can use splatting so new-aduser isn't incredibly long...here is an example.
$Params = @{
Name = $DisplayName
SamAccountName = $SamAccountName
UserPrincipalName = $UserPrincipalName
GivenName = $GivenName
Surname = $Surname
DisplayName = $DisplayName
Path = $Path
AccountPassword = $Password
Enabled = $True
ChangePasswordAtLogon = $True
HomeDirectory = $HomeDirectory
ScriptPath = $ScriptPath
EmployeeID = $EmployeeID
Department = $Department
Division = $Division
New-ADUser @Params
The splatting will definitely help, but I think what the OP is really looking for is a wrapper to make the command line really simple. I have a function I use called Copy-AdUser that does several things internally to support our business needs. The command
line is simple:
Copy-AdUser -Instance gwashing -GivenName Abraham -SurName Lincoln -Title President -Location 'White House'
Now you may not need to copy an existing user, but the ideas are the same. You can accept basic information about the user as parameter values and then in the guts of the function you can calculate all the other attributes, group membership, etc. The function
looks something like this:
Function Copy-ADUser {
#.SYNOPSIS
# Creates new Active Directory user based on attributes #and group membership of an existing user
#.DESCRIPTION
# Creates an Active Directory user based on an existing #user's group membership and Organizational Unit.
# Requires that the MS Active Directory module is loaded.
#.EXAMPLE
# Copy-AdUser -Instance gwashing -GivenName Abraham #-SurName Lincoln -Title "President" -Location "White House"
[CmdletBinding()]
Param
# Template user. Copies parent OU and group membership
[Parameter(Mandatory=$true,Position=0)]
$Instance,
[Parameter(Mandatory=$True,Position=1)]
[String]$GivenName,
[Parameter(Mandatory=$True,Position=2)]
[String]$SurName,
[Parameter(Mandatory=$True,Position=3)]
[String]$Title,
# building code for primary location
[Parameter(Mandatory=$True,Position=4)]
[String]$Location,
# Optional. Department or division (IT, HR, etc.)
[Parameter(Mandatory=$False)]
[String]$Department,
# Optionally specify the SAMAccountName if it needs to be different than the standard formula
[Parameter(Mandatory=$False)]
[string]$SamAccountName = ($GivenName.tolower().substring(0,1) +
$SurName.ToLower().Substring(0, [System.Math]::Min(7, $SurName.Length))),
# Optionally specify the UPN prefix if it needs to be different.
# This will be used for email addresses and the UPN
[Parameter(Mandatory=$False)]
[string]$UPNPrefix = ($GivenName.tolower() + '.' + $SurName.tolower())
# company specific variables
$EmailDomain = '@domain.com'
$HomeFolderPath = '\\Server\Share'
# calculate stuff
$Instance = Get-ADUser $Instance -Properties *
$OU = $Instance |
Select-Object @{n='ParentContainer';e={$_.DistinguishedName -replace "CN=$($_.cn),",''}}
$ADGroups = $Instance.MemberOf
$UserPrincipalName = ($UPNPrefix + $EmailDomain)
$FirstLast = "$GivenName $SurName"
$Proxies = "SMTP:$UserPrincipalName"
$HomeFolder = "$HomeFolderPath\$($SAMAccountName.ToUpper())"
# Create the Active Directory User
If(Get-ADUser -Filter {SAMAccountName -eq $SAMAccountName})
Write-Warning "User with name $SamAccountName already exists!
Please try again and specify unique values for the -samaccountname and -UPNPrefix parameters."
Return
Else
$Attributes = @{
Name = $FirstLast
GivenName = $GivenName
SurName = $SurName
UserPrincipalName = $UserPrincipalName
SAMAccountname = $SAMAccountName
AccountPassword = (Read-host -assecurestring 'Enter Password')
DisplayName = $FirstLast
Description = "$Location $Title"
EmailAddress = $UserPrincipalName
homedrive = 'H:'
HomeDirectory = $HomeFolder
ScriptPath = 'login.bat'
Title = $Title
Company = $Location
Department = $Department
path = $OU.ParentContainer
Enabled = $True
OtherAttributes = @{proxyaddresses=$Proxies}
New-Aduser @Attributes
# Add User to the same groups as the UserToCopy
$ADGroups |
ForEach-Object {
Add-ADGroupMember -Identity $_ -Members $SAMAccountName
# Create home folder and set permissions
Do
Set-FolderPermission -Path $HomeFolder -User $SamAccountName -Permission 'Fullcontrol' -ErrorAction SilentlyContinue
Until
(Get-ACL $Homefolder).Access.IdentityReference -like "*$SamAccountName*"
Please note this is probably not ready for you to use. You can change any of the internal logic to support naming schemes and paths that your organization uses. Also, there is a call to a function at the bottom that creates a home folder and sets permissions
on that folder. I haven't included that function so this part will error out.

Similar Messages

  • I bought an iPad 2 to replace my iPad. How do I get my apps to transfer to the new ipad2?

    I bought an iPad 2 to replace my iPad. How do I get my apps to transfer to the new ipad2?

    Have a look at this Discussion
    https://discussions.apple.com/message/16029216#16029216
    And there is always this...
    Download Past Purchases
    http://support.apple.com/kb/HT2519
    Log into iTunes using the account the Purchase was made with... Click on Buy... and a notice will come up saying you already have it... do you wish to download it again... Click Yes...

  • Just bought Photoshop Elements 13...I'm trying to make a slideshow but can't figure out how to alter duration time that the slide is on the screen.  They presently move from one to another way too quickly...also need a different pan and zoom option.  Wher

    Just bought Photoshop Elements 13...I'm trying to make a slideshow but can't figure out how to alter duration time that the slide is on the screen.  They presently move from one to another way too quickly...also need a different pan and zoom option.  Where are all the options I had in PS10?  Also...Can I burn this to a DVD?

    The changes have brought improvements but also drawbacks compared with the old slideshow editor.
    The templates are now fairly fixed but I find the “Classic Dark” gives reasonable results with some panning and you can click the audio button and browse you PC for any track. Unfortunately there are only three speed choices linked to the music track. The improvement for most people is that you can now export to your hard drive at 720p or 1080p and upload to sites like YouTube and Vimeo.

  • How to delete an  element in an array...(simple way)

    hi,
    Newbie here... please help how to delete an element in an array list?
    there are alot of codes but they're complicated for a newbie like me..
    (simple codes would be better ..:) thank you...

    makk_88 wrote:
    since u want the simple method.....
    just overrite the position of the element that need to be deleted with the next element in the array..
    by doing this, element need to be deleted will not be accessibe..
    i think u got..what i'm saying.
    thnx..Mak,
    Say we wish to delete the second element of an array a[1]. Do you really believe that a[1] = a[2] somehow magically deletes a[1]? I really don't think so Tim.
    Two eggs, minus 1, equals two eggs? Interesting theory, but I think you're probably wrong.
    *@OP:* My advise is just use an ArrayList.
    Cheers. Keith.

  • I did not deactivate a photoshop cs6 before there was a new hard drive installed with a Win7 Pro fresh install too. How can I activate photoshop on the new hardware if I can't deactivate it on the old hardware that is no longer available? Any help will be

    I did not deactivate a photoshop cs6 before there was a new "C" hard drive installed on my computer and a Win7 Pro fresh install too. How can I activate photoshop on the new hardware if I can't deactivate it on the old hardware that is no longer available? Any help will be appreciated.

    If it's the same machine (same CPU) there should not be any need to activate.  The actiovation is tied to your CPU.
    If you run into problems, then:
    Unfortunately, only Adobe customer service can assist you with your issue. These are user forums; you are not addressing Adobe here.
    Click on the link below, and after that click on "Still need Help? Contact us."
    Then on the next page, click Chat
    There is also a phone option. 1 (800) 833-6687
    http://helpx.adobe.com/contact.html?step=PHXS_downloading-installing-setting-up_licensing- activation

  • How can I sort lines of data (alphabetically) in the new pages 5.0?

    How can I sort lines of data (alphabetically) in the new pages 5.0?
    It was very simple to do in the previou version of pages.

    I'm not sure why people are torturing themselves with Pages 5 given the nearly universal outcry on this forum and the incredibly negative reviews elswhere, such as the App Store.
    Pages 4.3 should still be in an iWorks folder within your Applications folder.  Just use that.
    Please remember to tell Apple what we need from them at:
    http://www.apple.com/feedback/pages.html

  • One laptop died.  I have a new laptop now.  How can I get my Ipod song list on the new laptop without downloading each c.d.?

    My first laptop died.  I have a new laptop now.  How can I get my Ipod song list on the new laptop without downloading each c.d.?

    See this older post from another forum member Zevoneer covering the different ways to copy content from your iPod to your PC.
    https://discussions.apple.com/thread/2452022?start=0&tstart=0
    B-rock

  • I want to hook up a mac mini and us my tv as a monitor. how can I use airplay from my phone on the mac mini like i can with an apple tv (without having to buy an apple tv!). the airplay capabilities i want to use are mirroring and streaming from my iphone

    i want to hook up a mac mini and us my tv as a monitor. how can I use airplay from my phone on the mac mini like i can with an apple tv (without having to buy an apple tv!). the airplay capabilities i want to use are mirroring and streaming from my iphone!

    Download AirPlayer for Mac - Transforms your Mac into an AirPlay-compatible display. MacUpdate.com

  • How do I reorder songs in a playlist in the new itunes??? I can no longer just click and drag. When I click, it doesn't move!!!! Need help ASAP- trying to prepare for an aerobics class and need songs in a specific order!

    How do I reorder songs in a playlist in the new itunes??? I can no longer just click and drag. When I click, it doesn't move!!!! Need help ASAP- trying to prepare for an aerobics class and need songs in a specific order!

    Vera,
    Use View > View Options, and set 'Sort By" to "Manual Order."
    Then you will be able to drag-n-drop songs up and down the list.

  • How can I connect my existing firewire drives to the "new" Macbook pro

    I'm thinking about getting a new Macbook Pro, to take on the road.
    I already have four external drives with firewire and USB 2.0 ports.
    After I looked at the new machine I noticed it only had USB 3.0 and Thunderbolt ports.
    I dont see any Firewire to Thunderbolt technology out there nor do I see any  USB3.0 to anything technology. How can I connect my drives to the new system?

    You can use the USB3 connections for USB2.  That's downward compatible.  Just runs at USB2 speed.
    Just this week apple announced a couple of TB adapters, TB-to-ethernet and TB-to-firewire.  But the FW adapter is not at the apple store as yet (at least at the time I am posting this).

  • HT1711 I recently deleted iTunes off my computer by accident, when I reinstalled it I got the new version and I can't figure out how to get my music to store on my iTunes to transfer from my iPhone to iTunes.

    I recently deleted iTunes off my computer by accident, when I reinstalled it I got the new version and I can't figure out how to get my music to store on my iTunes to transfer from my iPhone to my iTunes.  I goal is to get my iPod filled with the music that I have stored on my iPhone and figured the only way to do so is to use iTunes as my transfer hub.  Any suggestions?

    Wow you're kind of a iTunes bully.  Dude the reason I am asking for advice and help is I am completely computer ********.  If I knew the answer I wouldn't be asking.  Anyways.  Question to anyone else. 
    If I have no music on my iTunes at all and no music on my computer, hard drive or any other version of storage yet there is a playlist on my iPhone. While my iPhone is plugged into my PC and my iTunes is up, is there a way to swap my playlist from my iPhone to my iTunes?  Directly?  Without downloading some new media source or backing up my old media source from my history.  The reason everything was deleted (iTunes and media source) was due to the size of everything and space it was requiring.  I am simply trying to get 233 songs from A to B. 
    I have tried magic but it is not working.   
    <Edited By Host>

  • Hardware missing -AGP Graphics card. How can I get my FCE 4 working on the new computer? Cheers

    Hi I have just upgraded my computer to Mac OSX 10.6.7 now I cant open my FCE 4. Message reads, Hardware missing -AGP Graphics card. How can I get my FCE 4 working on the new computer? Cheers

    Thanks, just so I know we are talking about the same items please let me know - When you say "run Prokit" do you mean downloaded? Which I did. I dont have Prokit 4.0.1 if you are referring to that, do I need this first? Please explain what application name I am looking for if other than Prokit which is sitting in my "download folder" do I need to manually put it into the apps folder? Sorry if the questions are layman but this area is not a very strong point for me. Thanks for your help so far.

  • I am going to buy a bigger hard drive. i am operating on Lion. how do i install my os on the new disk? i can't use time machine.

    i am going to buy a bigger hard drive. i am operating on Lion. how do i install my os on the new disk? i can't use time machine.

    Plug this new drive as an external drive, boot from lion recovery "command-R" more info here: http://www.apple.com/macosx/recovery/
    Open disk utility, format and partition the disk at your will (allways Mac OS plus...) and go ahead with the install, don't care if it tells you to whait thousands of hours, I did get installed in 2 hours and a half last week.
    If you can't plug the disk as an external one, you'll have first to do an USB disk recovery utility, as easy as download the recovery utility and install it in an USB disk, you can find anything from the link above, plug the disk as a single master, and boot from the USB you created before.
    Tell me how it went!!!

  • I try to open I photo and  I get the message, your iPhoto library is damaged...please restore from backup. But i din't made backup copy. How can i delete previous library and work wrom the new one?

    I try to open I photo and  I get the message, your iPhoto library is damaged...please restore from backup. But i din't made backup copy. How can i delete previous library and work wrom the new one?

    Try this (assuming you're using iPhoto 12): make a temporary, duplicate copy of the library and try the three fixes below in order as needed:
      Fix #1
    delete the iPhoto preference file, com.apple.iPhoto.plist, that resides in your Home/Library/Preferences folder. 
    delete iPhoto's cache files that are located in your Home/Library/Caches/com.apple.iPhoto folder.
    reboot, launch iPhoto and try again.
    NOTE: If you're moved your library from its default location in your Home/Pictures folder you will have to point iPhoto to its new location when you next open iPhoto by holding the the Option key.  You'll also have to reset the iPhoto's various preferences.
    Fix #2
    Launch iPhoto with the Command+Option keys depressed and follow the instructions to rebuild the library.
    Select options #1, #2 and #6. 
    Fix #3
    Rebuild the library using iPhoto Library Manager as follows:
    Using iPhoto Library Manager  to Rebuild Your iPhoto Library
    Download iPhoto Library Manager and launch.
    Click on the Add Library button, navigate to your Home/Pictures folder and select your iPhoto Library folder.
    Now that the library is listed in the left hand pane of iPLM, click on your library and go to the File ➙ Rebuild Library menu option
    In the next  window name the new library and select the location you want it to be placed.
    Click on the Create button.
    Note: This creates a new library based on the LIbraryData.xml file in the library and will recover Events, Albums, keywords, titles and comments but not books, calendars or slideshows. The original library will be left untouched for further attempts at fixing the problem or in case the rebuilt library is not satisfactory.
    OT

  • How can I  delete pictures from my moments off the new IOS8

    How can I delete pictures from my moments off the new IOS8 when it doesnt give me the option to delete the photo itself? The icon of the trash doesnt appear what so ever. This is the only thing im dissapointed in the IOS8 update. Theres not even a camera roll where you can delete the photo like the IOS7

    Try this:
    1. Start from the homescreen of your Iphone/pod (for dummies: where all of your "apps" are).
    2. Look for an app on your homescreen titled "Photos", when you find it - Click it.
    3. When the app opens, go ahead and look towards the bottom of your phonescreen.
         You should see a navigation with three different options;
           "Photos", "Shared" & "Albums"; Why don't you just go ahead and click on "Albums", there we go - Very nice!
    4. On the next screen that appears, it should display your different albums.
         Look for "Recently Deleted", and when you find it - Click on it.
           It should be on that list somewhere (or there's a problem!).
    5. Now you should see a screen displaying all of the recent photos you've deleted.
         The term we know as "recent" to iOS is thirty days (that's 720 hours in people-time!).
           You will have to click to each individual photo, however at the bottom left-hand cornor of every photo take notice of the 'Delete' option.
    You're welcome.

Maybe you are looking for