Skip to content

Instantly share code, notes, and snippets.

@KyleOndy
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save KyleOndy/5d4a769a8f8a8082d21b to your computer and use it in GitHub Desktop.

Select an option

Save KyleOndy/5d4a769a8f8a8082d21b to your computer and use it in GitHub Desktop.
Append-Groups
(Get-ADUser -Identity $CopyGroupsFrom -Properties Memberof).MemberOf | Add-ADGroupMember -Members $CopyGroupsTo
<#
.Synopsis
Copies all groups from one user to another
.DESCRIPTION
Queries all the user's groups from Active directory and and appends those groups to another user
.EXAMPLE
Append-Groups -CopyFrom UserA -CopyTo UserB
.NOTES
Must have RAST installed.
#>
function Append-Groups
{
[CmdletBinding(SupportsShouldProcess=$true,
PositionalBinding=$false,
HelpUri = 'http://www.microsoft.com/',
ConfirmImpact='Medium')]
Param
(
# User that is a member of the groups you wish to copy
[Parameter(Mandatory=$true,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$CopyFrom,
# User who needs the groups
[Parameter(Mandatory=$true,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$CopyTo
)
Begin
{
# Are these valid users in AD?
Get-ADUser $CopyFrom -ErrorAction Stop | Out-Null
Get-ADUser $CopyTo -ErrorAction Stop | Out-Null
}
Process
{
(Get-ADUser -Identity $CopyFrom -Properties Memberof).MemberOf | ForEach-Object {
if ($pscmdlet.ShouldProcess("$($_)", "Add $($CopyTo) to group"))
{
Add-ADGroupMember -Members $CopyTo -Identity $_
}
}
}
End
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment