Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get-content vs. import-csv and array slicing

1,081 views
Skip to first unread message

Neil Hobson [MVP]

unread,
Sep 21, 2009, 7:40:40 AM9/21/09
to
Hello,

I had a simple PS script that used get-content to read in a txt file
containing a list of mailbox aliases. It took the aliases in batches of 10,
i.e.

$users = get-content users.txt
$batch = $users[0..9]

users.txt would have contained just one mailbox alias per line and this
worked well. Obviously, I used a loop instead of [0..9] because the
users.txt file had many hundreds of entries, but you get the idea

Now I need to work with users.txt that contains other information in CSV
format, like:

alias,dn,name
neilh,"cn=neil hobson,cn=users,dc=domain,dc=com",neil hobson
johnd,"cn=john doe,cn=users,dc=domain,dc=com",john doe
....etc

However, the key bit is that I'm only interested in extracting the alias
field that I can pipe into the move-mailbox Exchange cmdlet. I've tried to
use import-csv rather than get-content but the array slicing doesn't seem to
work in the same way, i.e.

$users = import-csv users.txt
$batch = $users[0..9].alias

In this case, $batch doesn't return any values. I can successfully read-in
single parts of the array via something like this:

$batch = $users[0].alias

but not batches of 10 which is what I need. The reason I need 10 at a time
is because I want to move 10 mailboxes at a time to the same target
database.

Am I missing something?

Thanks!

--
Neil Hobson
Exchange MVP
http://www.msexchange.org/Neil_Hobson
http://www.simple-talk.com/author/neil-hobson/

Chris Dent

unread,
Sep 21, 2009, 8:24:01 AM9/21/09
to

Hi Neil,

The array returned here:

$users[0..9].alias

Doesn't have a property-set as such. It's an array of objects that do,
so you need to access each element of the array then get the property.

Since you want that sent to Move-Mailbox this is probably the most
appropriate approach:

$users[0..9] | %{ Move-Mailbox $_.Alias # And stuff }

HTH

Chris

Neil Hobson [MVP]

unread,
Sep 21, 2009, 10:04:32 AM9/21/09
to
Thanks for that Chris, appreciated.

This gets me what I wanted regarding the alias portion into the move-mailbox
cmdlet, but now the move-mailbox cmdlet processes the mailboxes one at a
time rather than 10 at a time which kinda defeats the objective. I'm not
sure I understand why that would be, as the input ($users) clearly has
multiple values.

Thanks,

Neil

"Chris Dent" <ch...@noreply.null> wrote in message
news:%236LSoZr...@TK2MSFTNGP02.phx.gbl...

Chris Dent

unread,
Sep 21, 2009, 10:15:27 AM9/21/09
to

I didn't look, Move-Mailbox will take an array as input?

$AliasList = $users[0..9] | %{ $_.Alias }
Move-Mailbox $AliasList

Otherwise, can you show me how you called it before (with the old file
format)? Haven't had a need to use Move-Mailbox for anything large scale
yet.

Chris

Neil Hobson [MVP]

unread,
Sep 21, 2009, 10:36:33 AM9/21/09
to
Yes, move-mailbox takes an array nicely. With your guidance, this now
works:

$users = import-csv users.csv
$aliaslist = $users[0..9] | %{ $_.alias}
$aliaslist | move-mailbox etc etc

Thanks for the help!

Neil

"Chris Dent" <ch...@noreply.null> wrote in message

news:O7PD5XsO...@TK2MSFTNGP02.phx.gbl...

0 new messages