Solved

Powershell Scripting Issue

Posted on 2013-06-14
4
1,623 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 40

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article runs through the process of deploying a single EXE application selectively to a group of user.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Microsoft Office Picture Manager has a Picture Shortcuts pane that shows a list with the Recently Browsed folders. While creating my video Micro Tutorial here at Experts Exchange showing How to Install Microsoft Office Picture Manager in Office 2013…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

830 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