TechNet
Products
IT Resources
Downloads
Training
Support
Products
Windows
Windows Server
System Center
Microsoft Edge
Office
Office 365
Exchange Server
SQL Server
SharePoint Products
Skype for Business
See all products »
Resources
Channel 9 Video
Evaluation Center
Learning Resources
Microsoft Tech Companion App
Microsoft Technical Communities
Microsoft Virtual Academy
Script Center
Server and Tools Blogs
TechNet Blogs
TechNet Flash Newsletter
TechNet Gallery
TechNet Library
TechNet Magazine
TechNet Wiki
Windows Sysinternals
Virtual Labs
Solutions
Networking
Cloud and Datacenter
Security
Virtualization
Updates
Service Packs
Security Bulletins
Windows Update
Trials
Windows Server 2016
System Center 2016
Windows 10 Enterprise
SQL Server 2016
See all trials »
Related Sites
Microsoft Download Center
Microsoft Evaluation Center
Drivers
Windows Sysinternals
TechNet Gallery
Training
Expert-led, virtual classes
Training Catalog
Class Locator
Microsoft Virtual Academy
Free Windows Server 2012 courses
Free Windows 8 courses
SQL Server training
Microsoft Official Courses On-Demand
Certifications
Certification overview
Special offers
MCSE Cloud Platform and Infrastructure
MCSE: Mobility
MCSE: Data Management and Analytics
MCSE Productivity
Other resources
Microsoft Events
Exam Replay
Born To Learn blog
Find technical communities in your area
Azure training
Official Practice Tests
Support options
For business
For developers
For IT professionals
For technical support
Support offerings
More support
Microsoft Premier Online
TechNet Forums
MSDN Forums
Security Bulletins & Advisories
Not an IT pro?
Microsoft Customer Support
Microsoft Community Forums
Sign in
Home
Library
Wiki
Learn
Gallery
Downloads
Support
Forums
Blogs
Resources For IT Professionals
United States (English)
Россия (Pусский)
中国(简体中文)
Brasil (Português)
Skip to locale bar
Post an article
Translate this page
Powered by
Microsoft® Translator
Wikis - Page Details
First published by
Markus Vilcinskas
(Microsoft)
When:
19 Apr 2011 7:40 AM
Last revision by
Durval Ramos
When:
23 May 2014 6:11 PM
Revisions:
7
Comments:
4
Options
Subscribe to Article (RSS)
Share this
Engage!
Wiki Ninjas Blog
(
Announcements
)
Wiki Ninjas on Twitter
TechNet Wiki Discussion Forum
Can You Improve This Article?
Positively!
Click Sign In to add the tip, solution, correction or comment that will help other users.
Report inappropriate content using
these instructions
.
Wiki
>
TechNet Articles
>
How to Use PowerShell to Add a Member to a Group in FIM
How to Use PowerShell to Add a Member to a Group in FIM
Article
History
How to Use PowerShell to Add a Member to a Group in FIM
FIM ScriptBox Item
Summary
This script adds a member to a group
Script Code
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
#--------------------------------------------------------------------------------------------------------------------
Set-Variable
-Name
GROUPNAME
-Value
"<My Group>"
-Option
Constant
Set-Variable
-Name
USERNAME
-Value
"<My User>"
-Option
Constant
Set-Variable
-Name
URI
-Value
"http://localhost:5725/resourcemanagementservice"
-Option
Constant
#--------------------------------------------------------------------------------------------------------------------
Function
GetSingleResource
{
Param
(
$Filter
)
End
{
$exportResource
=
export-fimconfig
-uri
$URI
`
–onlyBaseResources
`
-customconfig
(
"$Filter"
)
`
-ErrorVariable
Err
`
-ErrorAction
SilentlyContinue
If
(
$Err
)
{
Throw
$Err
}
If
(
$exportResource
-eq
$null
)
{
Throw
"Resource not found: $Filter"
}
If
(
@(
$exportResource
)
.
Count
-ne
1
)
{
Throw
"More than one resource found: $Filter"
}
$exportResource
}
}
#--------------------------------------------------------------------------------------------------------------------
If
(
@(
Get-PSSnapin
|
Where-Object
{
$_
.
Name
-eq
"FIMAutomation"
}
)
.
count
-eq
0
)
{
Add-PSSnapin
FIMAutomation
}
Clear-Host
$ProgressPreference
=
"SilentlyContinue"
#--------------------------------------------------------------------------------------------------------------------
$ProgressPreference
=
"Continue"
Write-Progress
-Activity
"Retrieving group object"
`
-Status
"Please wait..."
`
-CurrentOperation
"Processing step 1 of 3"
$ProgressPreference
=
"SilentlyContinue"
$groupObject
=
GetSingleResource
-Filter
"/Group[DisplayName='$GROUPNAME']"
$ProgressPreference
=
"Continue"
Write-Progress
-Activity
"Retrieving user object"
`
-Status
"Please wait..."
`
-CurrentOperation
"Processing step 2 of 3"
$ProgressPreference
=
"SilentlyContinue"
$userObject
=
GetSingleResource
-Filter
"/Person[DisplayName='$USERNAME']"
$importChange
=
New-Object
Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
$importChange
.
Operation
=
0
$importChange
.
AttributeName
=
"ExplicitMember"
$importChange
.
AttributeValue
=
$userObject
.
ResourceManagementObject
.
ObjectIdentifier
$importChange
.
FullyResolved
=
0
$importChange
.
Locale
=
"Invariant"
$importObject
=
New-Object
Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
$importObject
.
ObjectType
=
"Group"
$importObject
.
TargetObjectIdentifier
=
$groupObject
.
ResourceManagementObject
.
ObjectIdentifier
$importObject
.
SourceObjectIdentifier
=
$groupObject
.
ResourceManagementObject
.
ObjectIdentifier
$importObject
.
State
=
1
$ImportObject
.
Changes
=
(
,
$ImportChange
)
$ProgressPreference
=
"Continue"
Write-Progress
-Activity
"Adding user to group"
`
-Status
"Please wait..."
`
-CurrentOperation
"Processing step 3 of 3"
$ProgressPreference
=
"SilentlyContinue"
$importObject
|
Import-FIMConfig
-Uri
$URI
-ErrorVariable
Err
-ErrorAction
SilentlyContinue
|
Out-Null
If
(
$Err
)
{
Throw
$Err
}
#--------------------------------------------------------------------------------------------------------------------
Write-Host
"Command completed successfully"
#--------------------------------------------------------------------------------------------------------------------
Trap
{
Write-Host
$_
.
Exception
.
Message
-foregroundcolor
white
-backgroundcolor
darkred
Write-Host
$_
.
Exception
.
GetType
(
)
.
FullName
-foregroundcolor
white
-backgroundcolor
darkred
Write-Host
"`n"
Exit
1
}
#--------------------------------------------------------------------------------------------------------------------
Note
To provide feedback about this article, create a post on the
FIM TechNet Forum
.
For more FIM related Windows PowerShell scripts, see the
FIM ScriptBox
See Also
PowerShell Portal
Wiki: Portal of TechNet Wiki Portals