Hi Guys, Been struggling with this one for a while now and looking for some assistance.
Essentially, we will have a csv file containing the users name, email address. I would like to use the Get-FileName function, open a dialog box, select my CSV and then using that, use the fact that email address matches the SamAccountName of the user and use this to remove them from a group, and add to another group.
Here is what I have but it returns: The argument is Null or Empty, so clearly im not passing the data correctly, or at all. I feel like im close to this working, but equally could be way out
################################
Import-Module ActiveDirectory
Expected CSV File Format
Name|Emailaddress
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = “CSV (*.csv)| *.csv”
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$users = Get-FileName
$UIDs = Foreach ($UPN in $users.EmailAddress) {
(Get-Aduser -filter {UserPrincipalName -eq $UPN}).SamAccountName
}
Remove-ADGroupMember -identity “Group1” -members $UIDs -confirm:$false
Add-ADGroupMember -identity “Group2” -members $UIDs -confirm:$false