Solved

Powershell Scripting Issue

Posted on 2013-06-14
4
1,599 Views
Last Modified: 2013-06-14
Hi - I'm trying to create a powershell script to create new active directory users.  Attached is my CSV file and script.  I'm receiving the following errors:

-DisplayName : The term '-DisplayName' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:19 char:6
+                     -DisplayName $DisplayName -EmailAddress $Email -Description $Description`
+                     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-DisplayName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-GivenName : The term '-GivenName' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:20 char:6
+                     -GivenName $FirstName -Surname $Surname -PassThru -Enabled $true -Path $OU`
+                     ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-GivenName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:21 char:6
+                     -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -force)`
+                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Please assist.
Book3.csv
script.txt
0
Comment
Question by:emeka57
4 Comments
 
LVL 39

Accepted Solution

by:
Adam Brown earned 500 total points
ID: 39248610
It looks like you have a carriage return or two in your new-aduser command. Remove those. Each command has to exist on a single line with no carriage returns.
0
 

Author Comment

by:emeka57
ID: 39248622
Did that and still receiving the same errors.  See below:

+                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-DisplayName : The term '-DisplayName' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:19 char:6
+                     -DisplayName $DisplayName -EmailAddress $Email -Description $Description`
+                     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-DisplayName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-GivenName : The term '-GivenName' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:20 char:6
+                     -GivenName $FirstName -Surname $Surname -PassThru -Enabled $true -Path $OU`
+                     ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-GivenName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\emeka\Google Drive\PowerShell\Bulk User Creation\BulkUserCreation.ps1:21 char:6
+                     -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -force)`
+                     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39248769
I can see lot of trailing space in your csv file, which probably causing the error..

Is the users listed in CSV got created without any issue?

Try this code...
Import-Module ActiveDirectory
Import-Module MSOnline
Set-ExecutionPolicy Unrestricted

	$Users = Import-Csv Book3.csv
	ForEach ($User in $Users)
	{
		$OU = $User.Path #"OU=Faculty,OU=Canarsie ACS (LS),DC=ascendlearning,DC=org" 
		$Name = $User.Name
		$GivenName = $User.FirstName
		$Surname = $User.LastName
		$SAM = $User.SAMAccountname
		$UPN = $User.UPN
		$Email = $User.EmailAddress
		$Description = $User.Description
                $Password = $User.Password
		$Group = $User.Group
		
		New-ADUser -Name $Name -SamAccountName $SAM -UserPrincipalName $UPN -DisplayName $Name -EmailAddress $Email -Description $Description -GivenName $GivenName -Surname $Surname -Enabled $true -Path $OU -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -force) -PassThru
		#Add-ADGroupMember -Identity $Group -Member $SAM
	}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 39248988
When I downloaded your CSV, I had to remove all the commas at the end of each line (actually just the header line was sufficient) in order for the import-csv to work.
When you use line continuation with backticks, typically you should have a space right before the backtick.  When I did that, and also correct "givenname" to "$givenname" in your new-aduser command, your script worked fine for me.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Adoption of Microsoft’s Enterprise Mobility and Security solution and Office 365 will re-order the File Sync and Share market Microsoft has stated that its Enterprise Mobility + Security (EMS) is the fastest growing product in the history of the …
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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