Assign roles to user accounts with Office 365 PowerShell
上一次修改主题:2016-06-22
Summary: Use Office 365 PowerShell and the Add-MsolRoleMember cmdlet to assign roles to user accounts.
You can quickly and easily assign roles to user accounts using Office 365 PowerShell by identifying the user account's Display Name and the role name.
Determine the following:
-
Which user accounts that you want to configure.
To specify the user account, you must determine its Display Name. To get a list accounts, use this command:
Get-MsolUser | Sort DisplayName | Select-Object DisplayName | More
This command lists the Display Name of your user accounts, sorted by the Display Name, one screen at a time. You can filter the list to a smaller set by using the Where cmdlet. Here is an example:
Get-MsolUser | Where DisplayName -like "John*" | Sort DisplayName | Select-Object DisplayName | More
This command lists only the user accounts for which the Display Name starts with "John".
-
Which roles you want to assign to each user account.
To display the list of available roles that you can assign to user accounts, use this command:
Get-MsolRole | Sort Name | Select-Object Name, Description
Once you have determined the Display Name of the account and the Name of the role, use these commands to assign a role to an account:
$dispName="<The Display Name of the account>" $roleName="<The role Name you want to assign to the account>" $userObject=Get-MsolUser | Where DisplayName -eq $dispName $userUPN=$userObject.UserPrincipalName Add-MsolRoleMember -RoleMemberEmailAddress $userUPN -RoleName $roleName
Copy the commands and paste them into Notepad. For the $dispName and $roleName variables, replace the description text, remove the < and > characters, and leave the quotes. Copy the modified lines and paste them into your Windows PowerShell window to run them. Alternately, you can use the Windows PowerShell Integrated Script Environment (ISE).
Here is an example of a completed command set:
$dispName="Scott Wallace" $roleName="SharePoint Service Administrator" $userObject=Get-MsolUser | Where DisplayName -eq $dispName $userUPN=$userObject.UserPrincipalName Add-MsolRoleMember -RoleMemberEmailAddress $userUPN -RoleName $roleName
See Add-MsolRoleMember for additional options that you can set.
For PowerShell basics, see the Microsoft PowerShell Tutorial & Training Course from Microsoft Virtual Academy. For PowerShell basics for Office 365, see Office 365 PowerShell 入门.
For additional help with this procedure, please post your question and the name of this topic in the PowerShell for Office 365 group of the Office 365 Yammer Network.