PowerShell Tip: Adding Users Without an Email Address
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]
$group.AddUser($user)
$site.Dispose()
trap [Exception] {
$site.Dispose()
}
}
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:
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:
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:
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% [?]
add user method is giving errors it does accept 1 argument
how to add user with email address
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.
I am truly pleased to glance at this webpage posts which includes lots of helpful data, thanks for providing these information.
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.
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.
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.
Amazing article and mentioned in detail. I really like it. Thanks for sharing.
Thanks for sharing this powerful tip on how to add users without creating an email address. Loldle