Skip to content

Instantly share code, notes, and snippets.

@irwins
Last active January 29, 2016 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irwins/4d38bba8b5c7737ee390 to your computer and use it in GitHub Desktop.
Save irwins/4d38bba8b5c7737ee390 to your computer and use it in GitHub Desktop.
<#
Author: I.C.A. Strachan
Version:
Version History:
Purpose: Backup User group membership to file on a per user base
#>
[CmdletBinding()]
param(
[string]
$csvFile='users.csv',
[string]
$exportFolder = '.\export\dsa\UserMemberOf\backup\',
[Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]
$Encoding = 'UTF8',
$Delimiter = "`t"
)
#region Verify folder exists
$LogDate = get-date -uformat '%d-%m-%Y'
if(!(test-path "$exportFolder\$logDate")) {
$null = New-Item "$exportFolder\$logDate" -ItemType Directory -Force
}
#endregion
#region Define Hashtables for splatting
$csvParam = @{
Path = ".\source\csv\$csvFile"
Delimiter = $Delimiter
Encoding = $Encoding
}
$exportParam = @{
Delimiter = $Delimiter
Encoding = $Encoding
NoTypeInformation = $true
}
#endregion
#region Main
Import-Csv @csvParam |
ForEach-Object{
Get-ADUser -Identity $_.SamAccountName -Properties MemberOf |
Select-Object -ExpandProperty Memberof |
Get-ADGroup |
Select-Object SamAccountName, DistinguishedName |
Export-Csv @exportParam -Path "$exportFolder\$logDate\$($_.SamAccountName).csv"
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment