INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Creating an Object from a text file

Creating an Object from a text file

(OP)
Hi guys!
I've just started learning Powershell and I'm really newbie at it! so sorry for that

but i have a big problem! this is my scenario:

I've created a text file that contains some first names and last names that has been separated by space like this:
Jack Nicholson
Tom Cruise
.
.
I want to create an object with this in order to add multiple users in A.D
I used these commands:
$array = get-content name.txt | foreach (
>>$name = @{}
>>$name.first , $name.last = $_.Split()
>>$name
>> )

But i received this error:
Missing closing ')' in expression.
At line:3 char:1
+ <<<< $name.first , $name.last = $_.Split()
+ CategoryInfo : ParserError: (CloseParenToken:TokenId) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInExpression


can you help me please? what's the problem ? what should i do?

RE: Creating an Object from a text file

Here is a working example:

names.ps1

CODE

# run:
# powershell -ExecutionPolicy RemoteSigned .\names.ps1 

$my_file = get-content .\names.txt

echo "* Using string split function:"
foreach($line in $my_file){
  # split line into array
  $line_arr = $line.split()
  # array elements
  $first_name = $line_arr[0]
  $last_name = $line_arr[1]
  echo "first name: '$first_name', last name: '$last_name'"
}

echo ""

echo "* Using regex split function:"
foreach($line in $my_file){
  # split line into array
  $line_arr = [regex]::split($line,"\s+")
  # array elements
  $first_name = $line_arr[0]
  $last_name = $line_arr[1]
  echo "first name: '$first_name', last name: '$last_name'"
} 

Output:

CODE

C:\Work\PowerShell>powershell -ExecutionPolicy RemoteSigned .\names.ps1
* Using string split function:
first name: 'Jack', last name: 'Nicholson'
first name: 'Tom', last name: 'Cruise'

* Using regex split function:
first name: 'Jack', last name: 'Nicholson'
first name: 'Tom', last name: 'Cruise' 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close