PowerShell Tip: Adding Users Without an Email Address

June 2 2010 7 comments

Here’s another handy quickie: we have a bunch of users we want to add as owners when building a SharePoint site, but the adduser STSADM command won’t accept an empty email address. We don’t have real addresses for the users, nor do we want to make something up.

The SharePoint API doesn’t require that users have email addresses, though, so we can work around the problem.


function Adduser([string]$siteUrl, [string]$userlogin,
    [string]$username, [string]$groupName)
    $site = new-object 'Microsoft.SharePoint.SPSite' $siteUrl
    $site.RootWeb.SiteUsers.Add($userlogin, "", $username, "")
    $user = $site.RootWeb.AllUsers[$userlogin]
    $group = $site.RootWeb.SiteGroups[$groupName]
    trap [Exception] {

By now, most of this should look rather familiar. The only new thing is the “trap” construct, which acts much like the “try/catch” construct familiar from C# and a number of other languages.

If you’re wondering about the duplicated Dispose() call, it’s because PowerShell doesn’t come with a “using” construct, or even “try/finally”, so it seems we’re left with the unfortunate alternative of repetitive code. There is a better way, but it’s beyond the scope of this blog — check out Adam Weigert’s implementations of try/catch/finally and using. In addition to being really handy, they serve as a great example of how flexible PowerShell is.

Back to our issue at hand, let’s define a list of the users we want to grant owner access to:

$users = @(
    ,('DOMAIN\janedoe', 'Jane Doe')
    ,('DOMAIN\johndoe', 'John Doe')
) | foreach { New-Object PSObject -prop @{ Login = $_[0]; Name = $_[1] } }

I don’t much care for repeating the property names on each line, so instead we’ll build the list as a simple, untyped nested list, but then transform it into a list of objects with named properties.

Now, let’s enumerate all the site collections in a web application:

$sites = [xml](stsadm -o enumsites -url 'http://webapplication')

and then work through the site collections, first querying for the full name of the owner group and then adding each user to that group:

$sites.Sites.Site | foreach {
    $site = $_.Url;
    $groups = [xml](stsadm -o enumgroups -url $site)
    $ownerGroup = ($groups.Groups.Group | where {$_.Name -match "Owners" })
    $users | foreach {
        AddUser $site $_.Login $_.Name $ownerGroup.Name

This is fine for a small number of users, for a larger number, you’ll want to modify AddUser so that it can process a list of users, and won’t have to re-open the site for every user.

That’s it for today. Have fun!

Popularity: 2% [?]

7 comments to “PowerShell Tip: Adding Users Without an Email Address”

  1. anamika mukherjee says:

    add user method is giving errors it does accept 1 argument

  2. rambarat says:

    how to add user with email address

  3. This hit marker is especially good for long distance situations.

    This is probably the most important move you have to
    make. To put things in perspective, the highest
    grossing film at the time, Star Wars, grossed 486 million netting a profit of 175 million.

  4. Poppy says:

    I am truly pleased to glance at this webpage posts which includes lots of helpful data, thanks for providing these information.

  5. A vacuum is the best of cleansing resources; it may also be the more expensive.
    You will find various kinds of hoover with several different characteristics.

    Therefore prior to buying a top-rated hoover be sure you know what kind of vacuum-cleaner is best for your own needs.

    Selecting the best vacuum-cleaner can be confounding.
    To make points better you should be aware of what the various kinds of hoover
    are, what the primary characteristics it’s possible to find on a vacuum-cleaner, and need you type of flooring you will be utilizing a
    vacuum cleaner on.

  6. Terra says:

    I think everything posted made a lot of sense. However, think about
    this, suppose you wrote a catchier post title?
    I mean, I don’t want to tell you how to run your website, however suppose you added
    something that grabbed folk’s attention? I mean PowerShell
    Tip: Adding Users Without an Email Address | SharePoint
    Blues is kinda boring. You could look at Yahoo’s home page and watch how they create post headlines
    to grab people to click. You might try adding a video or a related pic or
    two to get people excited about everything’ve got to say.
    Just my opinion, it would bring your website a
    little livelier.

  7. What’s up,I check your blogs named “PowerShell Tip: Adding Users Without an Email Address | SharePoint Blues” daily.Your story-telling style is awesome, keep doing what you’re doing! And you can look our website about proxy server list.

Leave a Reply