Powershell: Add users with logon script to AD group

Home / Powershell: Add users with logon script to AD group

Here is a script which allows you to search for users with a specific logon script and add them to an Active Directory group.

I am migrating from logon scripts to group policy mappings. The main problem is, that the users get their drives mapped via a logon script. The script name is defined in the according AD field. What I needed to have is a script to search for a logon script file and put them into an AD group. That group will later be used to define every single GPO for the drive mappings.
For simplicity: I will run this command per script, so that I do not need a mapping of script name and group name.

This script needs to run on a domain controller or you have to invoke it there.

So here is the script:
Get-ADUser -Filter 'ScriptPath -eq "logon_script_department.bat"' | Add-ADPrincipalGroupMembership -MemberOf "logon_script_group"

It first searches to AD user with logon_script_department.bat as logon script and adds them to the AD group logon_script_group. Change bot value according to your environment.