Get-FileName and removing AD Group Members

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 :slight_smile:

################################

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

Phil,

welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!

Then … when you post code, error messages, sample data or console output format it as code, please.

Here you can read how that works: Guide to Posting Code

You can go back and edit your existing post. You don’t have to create a new one. :wink:

Thanks in advance.

Your Get-FileName function does exactly this … it get’s the NAME of the file - not the content. So to fill in your variable $users you have to do an import … something like this:

$fileName = Get-FileName
$users = Import-Csv -Path $fileName